Android开发03—Android常用基本控件(上)
1. 文本控件介绍
1) TextView类
TextView类继承自View类。TextView控件的功能是向用户显示文本内容,同时可选择性的让用户编辑文本。其自身被设置为不可编辑,其子类EditText被设置为可以编辑。
2) EditText类
EditText继承自TextView。EditText与TextView最大的不同就是用户可以对EditText控件进行编辑,同时还可以为EditText设置监听器,用来检测用户输入是否合法。
实例1:接收用户输入的电子邮箱地址和电话号码:
color.xml
<?xml version="1.0" encoding="utf-8"?><resources> <color name="shadow">#fd8d8d</color></resources>
string.xml
<?xml version="1.0" encoding="utf-8"?><resources> <string name="hello">Hello World, AndroidTest!</string> <string name="app_name">AndroidTest</string> <string name="tvEmail">邮箱地址</string> <string name="etEmail">请输入电子邮件地址</string> <string name="tvPhone">电话号码</string> <string name="etPhone">请输入电话号码</string> <string name="etInfo">此处显示登记信息</string></resources>
main.xml
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:shrinkColumns="0,2" android:layout_width="fill_parent" android:id="@+id/tableLayout1" android:layout_height="fill_parent"> <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/tvEmail" android:ellipsize="end" android:autoLink="email" android:id="@+id/tvEmail"></TextView> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:selectAllOnFocus="true" android:hint="@string/etEmail" android:id="@+id/etEmail"></EditText> </TableRow> <TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/tvPhone" android:ellipsize="middle" android:autoLink="phone" android:id="@+id/tvPhone"></TextView> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="@string/etPhone" android:selectAllOnFocus="true" android:maxWidth="160px" android:phoneNumber="true" android:singleLine="true" android:id="@+id/etPhone"></EditText> </TableRow> <EditText android:id="@+id/editText3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="EditText" android:editable="false" android:hint="@string/etInfo" android:cursorVisible="false" android:lines="5" android:shadowColor="@color/shadow" android:shadowDx="2.5" android:shadowDy="2.5" android:shadowRadius="5.0"></EditText> </TableLayout>
MyAndroidTest.java
package qijia.si;import android.app.Activity;import android.os.Bundle;import android.view.KeyEvent;import android.view.View;import android.view.View.OnKeyListener;import android.widget.EditText;public class AndroidTest extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); EditText etEmail = (EditText) findViewById(R.id.etEmail); etEmail.setOnKeyListener(myOnKeyListener); } private OnKeyListener myOnKeyListener = new OnKeyListener(){ public boolean onKey(View v,int keyCode, KeyEvent event){ EditText etInfo = (EditText) findViewById(R.id.editText3); EditText etEmail = (EditText) findViewById(R.id.etEmail); etInfo.setText("您输入的邮箱地址是:"+etEmail.getText()); return true; } }; }
2. 按钮控件
1) Button类简介
Button继承自TextView类,用户可以对Button控件执行按下或单击操作。Button控件的用法简单,主要是为Button控件设置View.OnClickListener监听器并在监听器的实现代码中开发按钮按下的事件。
一般格式:
public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btn = (Button) findViewById(R.Id.ID); btn.setOnClickListener(new OnClickListener){ public void onClick(View v){ //处理的事件 } };}
2) ImageButton类
ImageButton类继承自ImageView类,ImageButton控件与Button控件的主要区别在于,ImageButton类没有text属性,即按钮显示图片而不是文本。ImageButton控件设置按钮显示的图片通过android:src属性,也可以通过setImageResource(int)方法来实现。
默认情况下,ImageButton同Button一样具有背景色,当按钮处于不同状态喜爱时,背景色也会变化。当ImageButton所显示图片不能完全覆盖掉背景色时,这种显示效果会相当恶心,所以使用ImageButton一般要将背景色设置为其他图片或直接设置为透明。
注意:新建xml文件文件名不能是大写字母
实例2:为ImageButton按钮控件的不同状态设置不同的图片。
color.xml:
<?xml version="1.0" encoding="utf-8"?><resources> <color name="back">#000000</color></resources>
myselector.xml:
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="false" android:drawable="@drawable/back"/> <item android:state_pressed="true" android:drawable="@drawable/backdown"/> </selector>
main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:id="@+id/linearLayout1" android:layout_height="fill_parent" android:layout_width="fill_parent"> <ImageButton android:id="@+id/imageButton1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:background="@color/back" android:src="@drawable/myselector"></ImageButton> </LinearLayout>
3. 状态开关
1) ToggleButton类
ToggleButton的状态只能是选中和未选中,并且需要为不同的状态设置不同的文本。
android:Off 未被选中时显示的文本
android:On 选中时显示的文本
实例3:ToggleButton的用法:
strings.xml:
<?xml version="1.0" encoding="utf-8"?><resources> <string name="hello">Hello World, AndroidTest!</string> <string name="app_name">AndroidTest</string> <string name="off">关灯</string> <string name="on">开灯</string> </resources>
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"> <ImageView android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/bulb_off" android:id="@+id/ImageView01" android:layout_gravity="center_horizontal"></ImageView> <ToggleButton android:layout_gravity="center_horizontal" android:layout_height="wrap_content" android:layout_width="140dip" android:textOff="@string/on" android:textOn="@string/off" android:id="@+id/Tb"></ToggleButton> </LinearLayout>
MyAnroidTest.java:
package qijia.si;
import android.app.Activity;import android.os.Bundle;import android.view.KeyEvent;import android.view.View;import android.view.View.OnKeyListener;import android.widget.CompoundButton;import android.widget.CompoundButton.OnCheckedChangeListener;import android.widget.EditText;import android.widget.ImageView;import android.widget.ToggleButton;public class AndroidTest extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ToggleButton tb =(ToggleButton) findViewById(R.id.Tb); tb.setOnCheckedChangeListener(new OnCheckedChangeListener(){ //添加监听器 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){ setBulbState(isChecked); } }); } public void setBulbState(boolean state){ ImageView iv = (ImageView) findViewById(R.id.ImageView01); iv.setImageResource((state)?R.drawable.bulb_on:R.drawable.bulb_off); //设置图片资源 ToggleButton tb = (ToggleButton) this.findViewById(R.id.Tb); tb.setChecked(state); //设置为选中状态 } }
1 楼 bacaibangzi 2011-12-21
