Android自訂控制項之AlertDialog

來源:互聯網
上載者:User

最近公司沒什麼項目做,大部分時間都是自己在學習,而且覺得有必要和各位園友分享、交流下自己的所學所得,所以呢,決定今天開始寫博吧。

嗯嗯,步入正題,很多時候Android內建的控制項樣式不能滿足我們多樣化的需求,要自己去自訂才會給人耳目一新的感覺,今天就先拿AlertDialog開導,哈~先上(比較喜歡柯南O(∩_∩)O):

點擊enter按鈕會關閉對話方塊,留在當前Activity,點擊exit按鈕則退出應用。

首先是main.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="#FFFFFFFF"    android:orientation="vertical" >    <Button        android:id="@+id/button"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/hello" /></LinearLayout>

主Activity代碼CustomAlertDialogActivity.java:

package nbe.sense7.vinci.custom.alertdialog;import android.app.Activity;import android.app.AlertDialog;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.Window;import android.widget.Button;import android.widget.ImageButton;public class CustomAlertDialogActivity extends Activity {    /** Called when the activity is first created. */    private Button button;        @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                //點擊彈出自訂對話方塊        button = (Button)findViewById(R.id.button);        button.setOnClickListener(new OnClickListener(){            @Override            public void onClick(View v) {                // TODO Auto-generated method stub                showCustomAlertDialog();            }        });    }        private void showCustomAlertDialog(){        final AlertDialog alertDialog = new AlertDialog.Builder(this).create();        alertDialog.show();        Window win = alertDialog.getWindow();        //設定自訂的對話方塊布局        win.setContentView(R.layout.custom_alertdialog);                //關閉對話方塊按鈕事件        ImageButton enterBtn = (ImageButton)win.findViewById(R.id.enter_btn);        enterBtn.setOnClickListener(new OnClickListener(){            @Override            public void onClick(View v) {                // TODO Auto-generated method stub                alertDialog.cancel();            }        });                //退出程式        ImageButton exitBtn = (ImageButton)win.findViewById(R.id.exit_btn);        exitBtn.setOnClickListener(new OnClickListener(){            @Override            public void onClick(View v) {                // TODO Auto-generated method stub                CustomAlertDialogActivity.this.finish();            }        });    }}

自訂對話方塊布局檔案custom_alertdialog.xml:

<?xml version="1.0" encoding="UTF-8" ?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_height="wrap_content"    android:layout_width="wrap_content"    android:layout_margin="15dp"    android:gravity="center_horizontal"    android:orientation="horizontal"    android:background="@drawable/dialog_bg">        <!-- enter button -->    <ImageButton         android:id="@+id/enter_btn"        android:layout_height="wrap_content"        android:layout_width="wrap_content"        android:layout_margin="15dp"        android:layout_gravity="bottom"        android:src="@drawable/enter_btn"/>        <!-- quit button -->    <ImageButton         android:id="@+id/exit_btn"        android:layout_height="wrap_content"        android:layout_width="wrap_content"        android:layout_margin="15dp"        android:layout_gravity="bottom"        android:src="@drawable/exit_btn"/>    </LinearLayout>
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.