Android custom of AlertDialog

 

Recently the company nothing to do projects, most of the time in their own learning, and feel the need to park and you friends to share, exchange what they have learned under income, so, it decided to start blogging today.

Er er, come to the question, often comes with Android control styles can not meet our diverse needs, it has to go from the definition will be giving a fresh feel, today acquire AlertDialog enlighten, ha ~ the first renderings ( prefer Conan O (∩_∩) O):

Click on the enter button will close the dialog, stay on the current Activity, click the exit button to exit the application.

First main.xml:

Copy the code
<?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>
Copy the code

Main Activity Code CustomAlertDialogActivity.java:

Copy the code
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);
        
        // Click the Customize dialog box pops up 
        the Button = (the 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();
        Win the Window = alertDialog.getWindow ();
         // Setup dialog box custom layout 
        win.setContentView (R.layout.custom_alertdialog);
        
        // close the dialog button event 
        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();
            }
        });
        
        // Exit the program 
        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();
            }
        });
    }
}
Copy the code

Customize dialog box layout file custom_alertdialog.xml:

Copy the code
<?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>

Reproduced in: https: //www.cnblogs.com/Codenewbie/archive/2013/03/21/2973546.html

Guess you like

Origin blog.csdn.net/weixin_33912445/article/details/93448075