android学习之4种点击事件的响应方式

   如题,下面就一一列出对点击事件响应的4种方式:

第一种:内部类的形式:

   

package com.example.dail;

import android.net.Uri;
import android.os.Bundle;
import android.renderscript.Int2;
import android.text.TextUtils;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

	private Button bt_dail = null;
	private EditText et_number = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		bt_dail = (Button) findViewById(R.id.bt_dail);
		et_number = (EditText) findViewById(R.id.et_number);
		bt_dail.setOnClickListener(new MyListener());
	}
	private void callPhone() {
		String number = et_number.getText().toString();
		if(TextUtils.isEmpty(number)) {
			Toast.makeText(MainActivity.this, "号码不能为空", 1).show();
		}
		Intent intent = new Intent();
		intent.setAction(Intent.ACTION_CALL);
		intent.setData(Uri.parse("tel:"+number));
		startActivity(intent);
	}
	private class MyListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			callPhone();
		}
		
	}

}
  创建一个内部类MyListener实现OnClickListener接口。

第二种:匿名内部类:

package com.example.dail;

import android.net.Uri;
import android.os.Bundle;
import android.renderscript.Int2;
import android.text.TextUtils;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

	private Button bt_dail = null;
	private EditText et_number = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		bt_dail = (Button) findViewById(R.id.bt_dail);
		et_number = (EditText) findViewById(R.id.et_number);
		bt_dail.setOnClickListener(new OnClickListener(){
			public void onClick(View v){
				callPhone();
			}
		});
	}
	private void callPhone() {
		String number = et_number.getText().toString();
		if(TextUtils.isEmpty(number)) {
			Toast.makeText(MainActivity.this, "号码不能为空", 1).show();
		}
		Intent intent = new Intent();
		intent.setAction(Intent.ACTION_CALL);
		intent.setData(Uri.parse("tel:"+number));
		startActivity(intent);
	}
}
第三种:在xml布局文件中,定义onclick的方式

<Button
	    android:onClick="btOnClick"
	    android:id="@+id/bt_dail"
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:layout_below="@id/et_number"
	    android:text="@string/dail"/>"

然后再主类里面实现btOnClick方法,

private void btOnClick(View v) {
		callPhone();
}
private void callPhone() {
		String number = et_number.getText().toString();
		if(TextUtils.isEmpty(number)) {
			Toast.makeText(MainActivity.this, "号码不能为空", 1).show();
		}
		Intent intent = new Intent();
		intent.setAction(Intent.ACTION_CALL);
		intent.setData(Uri.parse("tel:"+number));
		startActivity(intent);
}

第四种:让主类实现OnClickListener接口,然后再主类实现未实现的方法

package com.example.dail;

import android.net.Uri;
import android.os.Bundle;
import android.renderscript.Int2;
import android.text.TextUtils;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

	private Button bt_dail = null;
	private EditText et_number = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		bt_dail = (Button) findViewById(R.id.bt_dail);
		et_number = (EditText) findViewById(R.id.et_number);
		bt_dail.setOnClickListener(this);
	}
	private void callPhone() {
		String number = et_number.getText().toString();
		if(TextUtils.isEmpty(number)) {
			Toast.makeText(MainActivity.this, "号码不能为空", 1).show();
		}
		Intent intent = new Intent();
		intent.setAction(Intent.ACTION_CALL);
		intent.setData(Uri.parse("tel:"+number));
		startActivity(intent);
	}
	@Override
	public void onClick(View v) {
		callPhone();		
	}
}
如上所述,4种点击事件的响应方式就是如此。


<pre code_snippet_id="103066" snippet_file_name="blog_20131208_4_3031669" name="code" class="java">
 
<pre code_snippet_id="103066" snippet_file_name="blog_20131208_5_5713865" name="code" class="java"><pre code_snippet_id="103066" snippet_file_name="blog_20131208_5_5713865" name="code" class="java"><pre code_snippet_id="103066" snippet_file_name="blog_20131208_5_5713865">
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

                
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值