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的方式

然後再主類裡面實現btOnClick方法,

private void btOnClick() {
		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種點擊事件的響應方式就是如此。
























發佈留言