Android開發之按鈕的四種點擊事件

以一個電話撥號器做案例來說明按鈕的四種點擊事件。

佈局文件:activity_main.xml

<relativelayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".MainActivity">
 
    <textview android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:textsize="19sp" android:text="請輸入手機號">
 
    <edittext android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_below="@+id/textView1" android:layout_margintop="14dp" android:ems="10">
 
        <requestfocus>
    </requestfocus></edittext>
 
    <button android:onclick="callPhone()" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignleft="@+id/editText1" android:layout_below="@+id/editText1" android:layout_margintop="39dp" android:text="撥打">
 
</button></textview></relativelayout>

第一種實現方案:

private EditText et_number;

	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //加載一個佈局
        setContentView(R.layout.activity_main);
        et_number = (EditText) findViewById(R.id.editText1); 
        //找到按鈕
        Button btn_call = (Button) findViewById(R.id.button1);
        //給button按鈕設置一個點擊事件
        btn_call.setOnClickListener(new MyClicklistener());
    }

    //定義一個類去實現按鈕需要的接口類型
    private class MyClicklistener implements OnClickListener{

    	//當按鈕被點擊的時候調用
		@Override
		public void onClick(View v) {
			//獲取edittext 文本內容
			String number = et_number.getText().toString().trim();
			if("".equals(number)){
				Toast.makeText(MainActivity.this,"number不能為空",Toast.LENGTH_LONG).show();
				return;
			}
			Intent intent = new Intent();//創建一個意圖對象
			//設置動作
			intent.setAction(Intent.ACTION_CALL);
			intent.setData(Uri.parse("tel:" + number));
			//開啟意圖
			startActivity(intent);
		}
	}

第二種:

private EditText et_number;
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_number = (EditText) findViewById(R.id.editText1); 
 
        Button btn_call = (Button) findViewById(R.id.button1);
        
        // 按鈕第二種點擊事件
        btn_call.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//實現撥打電話的邏輯
				callPhone();
			}
		});
    }

	private void callPhone() {
		//edittext 文本內容
		String number = et_number.getText().toString().trim();
		if("".equals(number)){
			Toast.makeText(MainActivity.this,"number不能為空",Toast.LENGTH_LONG).show();
			return;
		}
		//進行撥打電話
		Intent intent = new Intent();
		intent.setAction(Intent.ACTION_CALL);
		intent.setData(Uri.parse("tel:" + number));
		startActivity(intent);
	}

第三種:

private EditText et_number;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		et_number = (EditText) findViewById(R.id.editText1);

		Button btn_call = (Button) findViewById(R.id.button1);
		btn_call.setOnClickListener(this);

	}
 
	private void callPhone() {
		String number = et_number.getText().toString().trim();
		if ("".equals(number)) {
			
			Toast.makeText(MainActivity.this, "number不能為空", Toast.LENGTH_LONG).show();
			return;
		}
	
		Intent intent = new Intent();
		intent.setAction(Intent.ACTION_CALL);
		intent.setData(Uri.parse("tel:" + number));
		startActivity(intent);
	}

	@Override
	public void onClick(View v) {
		callPhone();
		}

	}

第四種:

private EditText et_number;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		et_number = (EditText) findViewById(R.id.editText1);
	}

	private void callPhone() {
		String number = et_number.getText().toString().trim();
		if ("".equals(number)) {
			Toast.makeText(MainActivity.this, "number不能為空", Toast.LENGTH_LONG).show();
			return;
		}
		Intent intent = new Intent();
		intent.setAction(Intent.ACTION_CALL);
		intent.setData(Uri.parse("tel:" + number));
		startActivity(intent);
	}

因為是電話撥號器,所以要在AndroidManifest.xml裡面加上撥打電話的權限

You May Also Like