OnClick的三种方式

来源:互联网 发布:网络病毒营销企业 编辑:程序博客网 时间:2024/06/05 05:04


View 点击事件的三种形式

2016年4月18日


import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Toast;public class MainActivity extends Activity implements OnClickListener {@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.main);findViewById(R.id.btn_done).setOnClickListener(this);//第一优先级、第一种 监听方法//findViewById(R.id.btn_done).setOnClickListener(new OnClickListener() {////@Override//public void onClick(View v) {//// TODO Auto-generated method stub//Toast.makeText(getApplicationContext(), "-- new OnClickListener()--", Toast.LENGTH_SHORT).show();//Log.e("", "new OnClickListener()");//}//});}//第三优先级、xml中参数需要注意 View v、xml中的监听方法public void myonclick(View v){Toast.makeText(getApplicationContext(), "--on click at xml--", Toast.LENGTH_SHORT).show();Log.e("", "on click at xml");}//第二优先级、实现接口的监听方法@Overridepublic void onClick(View v) {// TODO Auto-generated method stubToast.makeText(getApplicationContext(), "-- implements OnClickListener--", Toast.LENGTH_SHORT).show();Log.e("", " implements OnClickListener");}}

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >            <Button         android:id="@+id/btn_done"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:onClick="myonclick"        android:text="@string/abc_action_mode_done"        /></RelativeLayout>













0 0
原创粉丝点击