码迷,mamicode.com
首页 > 其他好文 > 详细

事件(1)

时间:2017-06-06 23:41:26      阅读:405      评论:0      收藏:0      [点我收藏+]

标签:匿名   parent   com   分享   rri   logs   roi   override   end   

事件三要素
  事件源:事件发生的来源
  事件:行为(点击,触摸...)
  监听器:当事件发送时,所要做的事情


 onClickListener(单击事件)
  组件.setOnClickListener(new OnClickListener(){
  @Override
    public void onClick(View v) {
      String str=et.getText().toString();
      tv.setText(str);
    }
  });

技术分享
 1 public class Click extends Activity{
 2     private Button bt;            //定义按钮
 3     private TextView tv;        //定义信息显示组件
 4     private EditText et;        //定义文本输入组件
 5     
 6     protected void onCreate(Bundle savedInstanceState) {
 7         super.onCreate(savedInstanceState);
 8         setContentView(R.layout.event);
 9         et=(EditText)findViewById(R.id.ete1);            //取得文本编辑组件
10         bt=(Button)findViewById(R.id.bte1);                //取得按钮
11         tv=(TextView)findViewById(R.id.tve1);            //取得文本显示组件
12         //设置监听器,匿名内部类
13         bt.setOnClickListener(new OnClickListener(){
14             @Override
15             public void onClick(View v) {
16                 String str=et.getText().toString();        //取得文本框输入内容
17                 tv.setText(str);                        //设置文本显示
18             }
19         });
代码示例
技术分享
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical"
 4     android:layout_width="fill_parent" 
 5     android:layout_height="fill_parent" 
 6     >
 7        <EditText
 8             android:id="@+id/ete1"
 9            android:layout_width="fill_parent" 
10             android:layout_height="wrap_content" 
11             android:background="#00FF00"
12       />
13         <Button
14             android:id="@+id/bte1"
15           android:layout_width="fill_parent" 
16                 android:layout_height="wrap_content" 
17             android:text="确定"
18      />
19      
20       <TextView
21           android:id="@+id/tve1"
22         android:layout_width="fill_parent" 
23                 android:layout_height="wrap_content" 
24            android:background="#FF0000"
25       />
26 </LinearLayout>            
xml文件代码

 

事件(1)

标签:匿名   parent   com   分享   rri   logs   roi   override   end   

原文地址:http://www.cnblogs.com/yang82/p/6953969.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!