android 控件响应的各种方法
来源:互联网 发布:JSforeach遍历二维数组 编辑:程序博客网 时间:2024/06/05 19:02
</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>}
- android 控件响应的各种方法
- Android 控件的各种方法介绍
- ANDROID控件添加事件响应的三种方法
- android 各种控件方法及属性的应用
- duilib进阶教程 -- 各种控件的响应 (10)
- 【Android基础篇】AlertDialog自定义setView方法的控件响应方法
- 【Android基础篇】AlertDialog自定义setView方法的控件响应方法
- android中各种控件,及其方法介绍
- android中各种控件,及其方法介绍
- 常用控件的事件响应方法
- 常用控件的事件响应方法
- 各种获取控件坐标的方法总结
- android中控件点击两次才响应onclick方法
- vlc-android 获取MediaPlayerEncounteredError,MediaPlayerBuffering等各种事件的响应
- vlc-android 获取MediaPlayerEncounteredError,MediaPlayerBuffering等各种事件的响应
- vlc-android 获取MediaPlayerEncounteredError,MediaPlayerBuffering等各种事件的响应
- Android各种控件的事件监听
- android-各种控件的监听事件 - 随心
- 设置文字左右中划线
- [漫画]程序员的日常生活 50
- VMfailVaild 错误号
- Hadoop2.6+zookeeper3.4.6+hbase1.1.0.1完全分布式配置方案
- [漫画]程序员的日常生活 51
- android 控件响应的各种方法
- 近期学习感悟
- 类集框架笔记2
- (二)黑马程序员——面向对象
- 罗永浩 2015跨年演讲《时间的朋友》
- NYOJ88汉诺塔(一)(快速幂&同余定理)
- iOS之实现倒计时功能
- 程序包管理yum&&编译篇
- iOS多线程开发其实很简单