Android自訂對話方塊,android自訂

來源:互聯網
上載者:User

Android自訂對話方塊,android自訂

在android中有內建的對話方塊,為了美觀,很多開發人員會使用自訂對話方塊,如:

點擊“彈出自訂對話方塊按鈕後”顯示效果。

首先要自己定義一個xml檔案定義自己對話方塊的樣式:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://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"            android:textStyle="bold" />    </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"            style="?android:attr/buttonStyleSmall"            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"            style="?android:attr/buttonStyleSmall"            android:layout_weight="1"            android:textColor="#FFFFFFFF"            android:background="@drawable/background"            android:text="取消"/>    </LinearLayout></LinearLayout>

 

mainActivity中:

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點擊後不能顯示IME的        dialog.getWindow().clearFlags(                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE                        | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);        dialog.getWindow().setSoftInputMode(                WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);        // *** 主要就是在這裡實現這種效果的.        // 設定視窗的內容頁面,shrew_exit_dialog.xml檔案中定義view內容        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);        // 為兩個button按鈕添加點擊的監聽事件        btn_ok.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View arg0) {                // TODO Auto-generated method stub                Toast.makeText(context, btn_ok.getText(), 0).show();            }        });        btn_delete.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View arg0) {                // TODO Auto-generated method stub                Toast.makeText(context, btn_delete.getText(), 0).show();            }        });    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.