Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 自定義對話框:打造美觀_unique的彈窗

編輯:關於 Android 開發

擺脫系統預設樣式,做一個有質感的彈窗

Android 自帶的對話框雖然能用,但樣式單調。為了更美觀,很多開發者會自定義對話框的佈局。下圖為點擊「彈出自定義對話框」按鈕後的效果:

一、自定義對話框佈局 auto_dialog.xml

先定義一個描述彈窗樣式的 XML,包含標題列、密碼輸入框與確定/取消兩個按鈕:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:orientation="vertical" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:orientation="horizontal" >
        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/airsign" />
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="3"
            android:gravity="center"
            android:text="請輸入密碼"
            android:textColor="#FFFFFFFF"
            android:textSize="32px" />
    </LinearLayout>
    <EditText
        android:id="@+id/et_pass"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:background="@drawable/tv_background"
        android:paddingLeft="10dp" />
    <LinearLayout
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <Button
            android:id="@+id/btn_ok"
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:layout_weight="1"
            android:background="@drawable/background"
            android:textColor="#FFFFFFFF"
            android:text="確定" />
        <Button
            android:id="@+id/btn_delete"
            android:layout_marginLeft="20dp"
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:layout_weight="1"
            android:textColor="#FFFFFFFF"
            android:background="@drawable/background"
            android:text="取消" />
    </LinearLayout>
</LinearLayout>

二、MainActivity 中彈出對話框

使用 AlertDialog.Builder 建立對話框,透過 setView() 載入自定義佈局。要特別注意:不加這行會導致自定義彈窗仍帶有原本的背景。另外,為了讓 EditText 點擊後能正常彈出輸入法,需要清除相關的 Window 旗標並設定軟輸入模式:

public class MainActivity extends Activity {
    private Context context = MainActivity.this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void clickView(View v) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        AlertDialog dialog = builder.create();
        // 沒有這句會導致自定義對話框仍保留原有背景
        builder.setView(View.inflate(this, R.layout.auto_dialog, null));
        dialog.show();

        // 讓 EditText 點擊後能顯示輸入法
        dialog.getWindow().clearFlags(
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        dialog.getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

        // 取得自定義佈局中的控件
        Window window = dialog.getWindow();
        window.setContentView(R.layout.auto_dialog);
        EditText et_pass = (EditText) window.findViewById(R.id.et_pass);
        final Button btn_ok = (Button) window.findViewById(R.id.btn_ok);
        final Button btn_delete = (Button) window.findViewById(R.id.btn_delete);

        btn_ok.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Toast.makeText(context, btn_ok.getText(), 0).show();
            }
        });
        btn_delete.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Toast.makeText(context, btn_delete.getText(), 0).show();
            }
        });
    }
}

關鍵在於用 window.setContentView() 把自定義佈局塞進對話框,再透過 window.findViewById() 取得各控件並綁定事件。掌握這個套路,就能做出各種風格化的彈窗。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved