編輯:關於 Android 程式設計
為控制項綁定點擊事件,Android 至少提供了四種做法。它們的效果相同,適用情境卻不太一樣,值得一次弄清楚。
android:onClick=" methodName ",方法簽名必須是 public void xxx(View v)。前兩者適合單一控制項的簡單情境;第三者在控制項眾多時會讓 onCreate 之外多出一個巨大的 switch;第四者最精簡,缺點是方法名寫死在 XML 裡,改名時容易被漏掉,編譯器不會提醒。
package com.example.test;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
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 {
Button bt_call;
private EditText tv_phonenumber;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt_call = (Button) findViewById(R.id.call);
tv_phonenumber = (EditText) MainActivity.this.findViewById(R.id.et_phonenumber);
// 寫法一:具名內部類別
// bt_call.setOnClickListener(new myListener());
// 寫法二:匿名內部類別
// bt_call.setOnClickListener(new OnClickListener() {
// @Override
// public void onClick(View v) {
// callphone();
// }
// });
// 寫法三:Activity 實作介面
// bt_call.setOnClickListener(this);
}
// 寫法四:由版面檔的 android:onClick 呼叫
public void callButtonClicked(View view) {
callphone();
}
private class myListener implements OnClickListener {
@Override
public void onClick(View v) {
callphone();
}
}
private void callphone() {
String number = tv_phonenumber.getText().toString().trim();
if (TextUtils.isEmpty(number)) {
Toast.makeText(MainActivity.this, "請輸入手機號碼", Toast.LENGTH_SHORT).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) {
switch (v.getId()) {
case R.id.call:
callphone();
break;
}
}
}
<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" >
<EditText
android:id="@+id/et_phonenumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:inputType="phone" />
<Button
android:id="@+id/call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@id/et_phonenumber"
android:layout_marginTop="16dp"
android:onClick="callButtonClicked"
android:text="@string/bt_call" />
</RelativeLayout>
這裡有兩個實務上的提醒。第一,Intent.ACTION_CALL 會直接撥出電話,需要 CALL_PHONE 權限,而且屬於敏感權限,在較新的 Android 版本上必須執行期動態申請;若只想跳到期待撥號畫面而不直接撥出,改用 Intent.ACTION_DIAL 就不需要任何權限。第二,輸入號碼後記得 trim() 並檢查空值,否則會帶著空白字元去組 URI。
深入 Android 7.0 源碼:Zygote 進程的啟動全解析
本文以 Android 7.0 源碼為依據,帶你完整走一遍 Zygote 進程從誕生到分化的來龍去脈。 說起 Zygote 的由來,其實它是 init 進程在解析 ini
Android 入門:AlertDialog 的兩種形態實例分析
本文談 AlertDialog,這是開發中最常遇到的對話框。要特別注意的是,它和 Win32 開發裡的 Dialog 概念不同——AlertDialog 是非阻塞的,彈出
ViewPager 引導頁最後一頁左滑跳轉主頁面的完整實現
引導頁的收尾交互通常是這樣:滑到最後一頁,再往左一推,就進入主界面。實現它需要兩個監聽配合。 addOnPageChangeListener:用來實時掌握當前頁序(pos
Android ViewPager 深入解析:從基礎滑動到 Fragment 實作
一、ViewPager 的含義 ViewPager 的核心功能,就是讓視圖可以左右滑動切換,就像我們在 Launcher 桌面左右滑動頁面那樣。它主要用來實現多頁面的切換