Android starter series-simple application of TextView EditText Button ImageView, textviewedittext
The first article is original. In fact, I am a newbie. Just share some basic knowledge. I hope it will be helpful.
TextView EditText Button ImageView may be the most common and basic controls in Android development.
This article introduces the usage of these controls from the simplest point of view (by default, you have mastered the establishment of the development environment, and you are still using eclipse to change your computer to use as at the end of the month; we recommend that you use as for development. Many open source resources only provide the as version)
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 // load the layout file 12 setContentView (R. layout. activity_main); 13 // initialize the control 14 init (); 15 // set the OnClickListener listener for the Button 16 btn. setOnClickListener (new OnClickListener () {17 @ Override18 public void onClick (View v) {19 // display the input content in et through Toast 20 Toast. makeText (MainActivity. this, 21 "et input:" + et. getText (). toString (), Toast. LENGTH_SHORT) 22. show (); 23} 24}); 25 // set the OnClickListener listener for ImageView 26 iv. setOnClickListener (new OnClickListener () {27 @ Override28 public void onClick (View v) {29 // when you click btn, change the image of iv to another image 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 <TextView12 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 <EditText22 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 <Button34 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 <ImageView47 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>
:
Summary:
Only some of the attributes of these controls are used in the xml layout file, which are the most basic attributes. For details about the attributes, see the official documentation.
At the same time, the Activity only listens to the OnClickListener of the Button and ImageView, and can listen to other events. You can try it on your own.