Android自定義控件之AlertDialog


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

嗯嗯,步入正題,很多時候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>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM