</pre><p><pre name="code" class="html">
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/color" />
<Button
android:id="@+id/button1"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_width="80sp"
android:text="@string/red" />
<Button
android:id="@+id/button2"
android:layout_width="80sp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/black" />
</LinearLayout>
第一种方法是使用setOnXXXListener()设置事件处理函数。
在获取句柄时需要转换成相应的控件类型,findViewById()函数的参数是一个整数,返回值是一个android.view.View类型。通过R.id.XXX找到布局文件中定义的ID,然后通过将基础类转换成其实际的类获得真正的句柄。注意:所转换类必须和布局文件中描述的控件一致。
SetOnXXXListener()等函数是android.view.View类的函数,各种控件(包括Button、EditText)都扩展这个类,同族的函数包括:
void setOnClickListener(View.OnClickListener l);
void setOnFocusChangeListener(View.OnFocusChangeListener l);
void setOnKeyListener(View.OnKeyListener l);
void setOnLongClickListener(View.OnLongClickListener l);
void setOnTouchListener(View.OnTouchListener l);
这些函数用于事件处理,它们由程序实现,通过设置这些内容也就设置了控件的行为。这些函数的参数都是所对应的android.view.View类中的方法。
Android中UI基本控制内容:使用findViewById()联系布局文件中控件和句柄,并通过OnClickListener()等定制句柄的行为。
public class testevent extends Activity {
<span style="white-space:pre"> </span>protected void onCreate(Bundle savedInstanceState)
<span style="white-space:pre"> </span>{
super.onCreate(savedInstanceState);
setContentView(R.layout.testevent);
final TextView textview = (TextView)findViewById(R.id.textView1);
final Button redButton = (Button)findViewById(R.id.button1);
final Button blackButton = (Button)findViewById(R.id.button2);
redButton.setOnClickListener(new View.OnClickListener()
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public void onClick(View arg0)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>// TODO Auto-generated method stub
<span style="white-space:pre"> </span>textview.setBackgroundColor(Color.RED);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>);
blackButton.setOnClickListener(new View.OnClickListener()
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public void onClick(View arg0)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>// TODO Auto-generated method stub
<span style="white-space:pre"> </span>textview.setBackgroundColor(Color.BLACK);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>);
<span style="white-space:pre"> </span>}
}
第二种方法是让活动实现(implements)了OnClickListener()这个接口,也就是需要实现其中的onClick()方法。然后通过setOnClickListener()将其设置到按钮中的参数就是this,表示了当前的活动。
public class testevent extends Activity implements OnClickListener {
<span style="white-space:pre"> </span>TextView textview;
Button redButton;
Button blackButton;
<span style="white-space:pre"> </span>protected void onCreate(Bundle savedInstanceState)
<span style="white-space:pre"> </span>{
super.onCreate(savedInstanceState);
setContentView(R.layout.testevent);
textview = (TextView)findViewById(R.id.textView1);
redButton = (Button)findViewById(R.id.button1);
redButton.setOnClickListener(this);
blackButton = (Button)findViewById(R.id.button2);
blackButton.setOnClickListener(this);
redButton.setOnClickListener(new View.OnClickListener()
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public void onClick(View arg0)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>// TODO Auto-generated method stub
<span style="white-space:pre"> </span>textview.setBackgroundColor(Color.RED);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>);
blackButton.setOnClickListener(new View.OnClickListener()
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public void onClick(View arg0)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>// TODO Auto-generated method stub
<span style="white-space:pre"> </span>textview.setBackgroundColor(Color.BLACK);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>);*/
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public void onClick(View v) {
<span style="white-space:pre"> </span>// TODO Auto-generated method stub
<span style="white-space:pre"> </span>switch (v.getId())
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>case R.id.button1:
<span style="white-space:pre"> </span>textview.setBackgroundColor(Color.RED);
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case R.id.button2:
<span style="white-space:pre"> </span>textview.setBackgroundColor(Color.BLACK);
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>default:
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}