第一篇原创,其实自己就是一菜鸟,简单分享点基本知识吧。希望能有所帮助吧。
TextView EditText Button ImageView 这几个控件可能是Android开发中最常用、最基本的几个控件
本篇文章就从最简单的角度介绍下这几个控件的用法(默认您已经掌握了开发环境的搭建,本吊还在用eclipse ,准备月底换电脑用 as;建议用as进行开发吧,好多开源资源只提供了as版本 )
MainActivity.java
1 public class MainActivity extends Activity { 2 3 private TextView tv; 4 private EditText et; 5 private Button btn; 6 private ImageView iv; 7 8 @Override 9 protected void onCreate(Bundle savedInstanceState) { 10 super.onCreate(savedInstanceState); 11 // 加载布局文件 12 setContentView(R.layout.activity_main); 13 // 初始化控件 14 init(); 15 // 设置Button的OnClickListener监听器 16 btn.setOnClickListener(new OnClickListener() { 17 @Override 18 public void onClick(View v) { 19 // 将et中输入的内容通过Toast显示出来 20 Toast.makeText(MainActivity.this, 21 "et的输入为:" + et.getText().toString(), Toast.LENGTH_SHORT) 22 .show(); 23 } 24 }); 25 // 设置ImageView的OnClickListener监听器 26 iv.setOnClickListener(new OnClickListener() { 27 @Override 28 public void onClick(View v) { 29 // 当点击btn的时候就让iv的图片变成另一张图片 30 iv.setBackgroundResource(R.drawable.animal); 31 } 32 }); 33 } 34 35 private void init() { 36 tv = (TextView) this.findViewById(R.id.acMain_tv_username); 37 et = (EditText) this.findViewById(R.id.acMain_et_password); 38 btn = (Button) this.findViewById(R.id.acMain_btn_login); 39 iv = (ImageView) this.findViewById(R.id.acMain_iv_show); 40 tv.setText("Hello , this is gh"); 41 }
activity_main.xml
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context="com.example.cnblogs001.MainActivity" > 10 11 <TextView 12 android:id="@+id/acMain_tv_username" 13 android:layout_width="match_parent" 14 android:layout_height="wrap_content" 15 android:gravity="center_horizontal" 16 android:padding="10dp" 17 android:text="@string/hello_world" 18 android:textColor="@android:color/holo_blue_dark" 19 android:textSize="20sp" /> 20 21 <EditText 22 android:id="@+id/acMain_et_password" 23 android:layout_width="match_parent" 24 android:layout_height="wrap_content" 25 android:layout_below="@id/acMain_tv_username" 26 android:layout_marginTop="10dp" 27 android:ellipsize="end" 28 android:gravity="center_horizontal" 29 android:hint="@string/hint_et_password" 30 android:inputType="textPassword" 31 android:maxLines="1" /> 32 33 <Button 34 android:id="@+id/acMain_btn_login" 35 android:layout_width="match_parent" 36 android:layout_height="wrap_content" 37 android:layout_below="@id/acMain_et_password" 38 android:layout_marginTop="10dp" 39 android:alpha="0.5" 40 android:background="@android:color/holo_blue_dark" 41 android:gravity="center" 42 android:text="@string/text_btn_login" 43 android:textColor="#000000" 44 android:textSize="20sp" /> 45 46 <ImageView 47 android:id="@+id/acMain_iv_show" 48 android:layout_width="150dp" 49 android:layout_height="150dp" 50 android:layout_below="@id/acMain_btn_login" 51 android:layout_centerHorizontal="true" 52 android:layout_marginTop="10dp" 53 android:background="@drawable/logo" 54 android:scaleType="fitCenter" /> 55 56 </RelativeLayout>
效果图:
总结:
xml布局文件中只用了这几个控件的部分属性,都是最基本的属性;详细属性可以查阅官方文档
同时在Activity中只 监听了 Button 和 ImageView 的OnClickListener ,还可以监听其他事件,大家可以自行尝试一下
android入门系列- TextView EditText Button ImageView 的简单应用
原文:http://www.cnblogs.com/nice-gh/p/4963993.html