android之AlertDialog

转自:http://www.2cto.com/kf/201205/131876.html

参考文章:http://blog.csdn.net/flyfight88/article/details/8602162

 

 

布局文件

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/withButton"
            android:onClick="doClick"
            android:text="带多个按钮的对话框" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/list"
            android:onClick="doClick"
            android:text="列表对话框" />
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/radioList"
            android:onClick="doClick"
            android:text="带单选按钮的列表对话框" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/checkboxList"
            android:onClick="doClick"
            android:text="带多选按钮的列表对话框" />
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/my"
            android:onClick="doClick"
            android:text="自定义"/>

    </LinearLayout>

 

public class MainActivity extends AppCompatActivity {

    private AlertDialog.Builder builder;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void doClick(View v){
        switch (v.getId()){
            case R.id.withButton:
            {
                builder=new AlertDialog.Builder(this);
                builder.setTitle("title");
                builder.setMessage("message");
                builder.setIcon(R.mipmap.ic_launcher);
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "确定删除", Toast.LENGTH_SHORT).show();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "取消删除", Toast.LENGTH_SHORT).show();
                    }
                });
                builder.setNeutralButton("忽略", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "忽略", Toast.LENGTH_SHORT).show();
                    }
                });

                builder.create().show();
                break;
            }
            case R.id.list:
            {
                final String[] arrayFruit = new String[] { "苹果", "橘子", "草莓", "香蕉" };

                builder=new AlertDialog.Builder(this).
                        setTitle("你喜欢吃哪种水果?").
                        setIcon(R.mipmap.ic_launcher).
                        setItems(arrayFruit, new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(MainActivity.this, arrayFruit[which], Toast.LENGTH_SHORT).show();
                            }
                        }).
                        setNegativeButton("取消", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // TODO Auto-generated method stub
                            }
                        });
                builder.create().show();
                break;
            }
            case R.id.radioList:
            {
                final String[] arrayFruit = new String[] { "苹果", "橘子", "草莓", "香蕉" };

                builder=new AlertDialog.Builder(this).
                        setTitle("你喜欢吃哪种水果?").
                        setIcon(R.mipmap.ic_launcher).
                        setSingleChoiceItems(arrayFruit, 0, new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(MainActivity.this, arrayFruit[which], Toast.LENGTH_SHORT).show();
                            }
                        });

                builder.create().show();
                break;
            }
            case R.id.checkboxList:
            { final String[] arrayFruit = new String[] { "苹果", "橘子", "草莓", "香蕉" };
                final boolean[] arrayFruitSelected = new boolean[] {true, true, false, false};

                builder=new AlertDialog.Builder(this).
                        setTitle("你喜欢吃哪种水果?").
                        setIcon(R.mipmap.ic_launcher).
                        setMultiChoiceItems(arrayFruit, arrayFruitSelected, new DialogInterface.OnMultiChoiceClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                                arrayFruitSelected[which] = isChecked;
                            }
                        });
                builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                StringBuilder stringBuilder = new StringBuilder();
                                for (int i = 0; i < arrayFruitSelected.length; i++) {
                                    if (arrayFruitSelected[i] == true)
                                    {
                                        stringBuilder.append(arrayFruit[i] + "、");
                                    }
                                }
                                String str=stringBuilder.toString().substring(0,stringBuilder.toString().length()-1).toString();
                                Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
                            }
                        }).create();
                builder.show();
                break;
            }
        }
    }
}

  

 

 

下面是自定义对话框

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:textSize="20sp"
            android:gravity="center_vertical"
            android:text="用户名:" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:textSize="20sp"
            android:gravity="center_vertical"
            android:text="密   码:" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:orientation="vertical" >

        <EditText
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:textSize="20sp"
            android:hint="邮箱"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:textSize="20sp"
            android:hint="你的密码一定要保密哟"/>
    </LinearLayout>


</LinearLayout>

  

public class MainActivity extends AppCompatActivity {

    private AlertDialog.Builder builder;
    private LayoutInflater Inflater;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void doClick(View v){
        switch (v.getId()){
            case R.id.my:
            {
                // 取得自定义View
                Inflater = LayoutInflater.from(this);
                View myLoginView = Inflater.inflate(R.layout.mylayout, null);

                builder = new AlertDialog.Builder(this).
                        setTitle("用户登录").
                        setIcon(R.mipmap.ic_launcher).
                        setView(myLoginView);
                builder.setPositiveButton("登录", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // TODO Auto-generated method stub
                            }
                        }).
                        setNegativeButton("取消", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // TODO Auto-generated method stub
                            }
                        }).
                        create();
                builder.show();
                break;
            }
        }
    }
}

  显示

posted @ 2016-07-29 20:05  324四哥  阅读(148)  评论(0编辑  收藏  举报