加入收藏 | 设为首页 | 会员中心 | 我要投稿 应用网_丽江站长网 (http://www.0888zz.com/)- 科技、建站、数据工具、云上网络、机器学习!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

Android四种监听技巧

发布时间:2021-12-18 16:28:37 所属栏目:PHP教程 来源:互联网
导读://main.xml文件 ?xml version=1.0 encoding=utf-8? LinearLayout xmlns:Android=http://schemas.android.com/apk/res/android android:layout_width=fill_parent android:layout_height=fill_parent android:orientation=vertical TextView android:layout_wi
//main.xml文件
 
<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    android:orientation="vertical" >  
  
  
    <TextView   
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:text="@string/title"  
        />  
    <EditText   
        android:id="@+id/myedittext"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:numeric="integer"  
        />  
    <Button   
        android:id="@+id/mybutton1"  
        android:layout_width="200dip"  
        android:layout_height="wrap_content"  
        android:text="@string/callout1"  
        />  
    <Button   
        android:id="@+id/mybutton2"  
        android:layout_width="200dip"  
        android:layout_height="wrap_content"  
        android:text="@string/callout2"  
        />  
          
    <Button   
        android:id="@+id/mybutton3"  
        android:layout_width="200dip"  
        android:layout_height="wrap_content"  
        android:text="@string/callout3"  
        />  
          
   <Button   
        android:id="@+id/mybutton4"  
        android:layout_width="200dip"  
        android:layout_height="wrap_content"  
        android:text="@string/callout4"  
        />  
  
  
</LinearLayout>  
------------------------------------------------------------------------------------------
 
//strings.xml文件
 
<?xml version="1.0" encoding="utf-8"?>  
<resources>  
  
 <string name="title">Android Call phone Application</string>  
 <string name="app_name">复习打出电话</string>  
<string name="callout1">拨打1</string>  
<string name="callout2">拨打2</string>  
<string name="callout3">拨打3</string>  
<string name="callout4">拨打4</string>  
</resources>  
------------------------------------------------------------------------------------------
 
 
 
//CallPhone2Activity.java 文件
 
package sn.len.callphone;  
  
import android.app.Activity;  
import android.os.Bundle;  
import android.util.Log;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
  
public class CallPhone2Activity extends Activity  implements OnClickListener   
{  
    @Override  
    public void onCreate(Bundle savedInstanceState)  
    {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        Button button1=(Button)findViewById(R.id.mybutton1);  
        Button button2=(Button)findViewById(R.id.mybutton2);  
        Button button3=(Button)findViewById(R.id.mybutton3);  
        Button button4=(Button)findViewById(R.id.mybutton4);  
        //注册监听   
        button1.setOnClickListener(this);  
        button2.setOnClickListener(new Button2Listener());  
          
        //方法三,匿名内部类实现事件监听器接口    
        //匿名内部类实现事件监听器   常用,如上同button2   
        button3.setOnClickListener  
        (  
                new View.OnClickListener()   
                {     
                    @Override  
                    public void onClick(View v)   
                    {  
                        Log.e("方法三", "匿名内部类实现事件监听器接口 ");  
                    }  
                }  
        );  
      //方法四,外部类实现事件监听器接口,很少用   
        button4.setOnClickListener(new callOut(this));  
          
    }  
    //方法一,自身实现监听器接口     
    //自身类实现事件监听器接口  对于同一下XXXListener callback只能有一个实现   
    @Override  
    public void onClick(View v)   
    {  
        // TODO Auto-generated method stub   
        Log.e("方法一", "自己实现监听");  
    }  
      
      
    //方法二,内部类实现事件监听接口     
    //内部类实现事件监听器  常用,可以方便的访问其外围类的任意成员,一个内部类在此也可以代表解决问题的一个操作   
    class Button2Listener implements OnClickListener  
    {  
  
        @Override  
        public void onClick(View v)   
        {  
            // TODO Auto-generated method stub   
            Log.e("方法二", "内部类实现事件监听接口");  
        }  
          
    }  
      
}  
备注:最常用为第二种和第三种。
 
------------------------------------------------------------------------------------------
 
//callOut.java 文件 -->第四种���法,外部类
 
package sn.len.callphone;  
  
import android.app.Activity;  
import android.util.Log;  
import android.view.View;  
import android.view.View.OnClickListener;  
  
public class callOut implements OnClickListener  
{  
    private Activity activity;  
    public callOut(Activity activity)  
    {  
        this.activity=activity;  
    }  
    @Override  
    public void onClick(View v)   
    {  
        //找ID   
        //TextView textView = (TextView) activity.findViewById(R.id.resultView);     
        Log.e("方法四", "外部类实现事件监听器接口 ");  
    }  
      
}  

(编辑:应用网_丽江站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读