Android中的AlertDialog使用示例二(普通选项对话框)

Layout界面代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:text="普通选项"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="show2"
        android:id="@+id/button2" />
</LinearLayout>

Java功能实现代码:

public class AlertDialogDemo extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.alertdialog);
    }
    public void show2(View v){
        //实例化建造者
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        //设置警告对话框标题
        builder.setTitle("选花魁喽");
        //定义警告框选择窗体的内容(这里用String数组)
        final String[] beautifulGirls = {"芙蓉姐姐","凤姐","范爷","我自己"};
        //将数组内的元素设置到dialog的每个item中,并做事件处理(这里用土司)
        builder.setItems(beautifulGirls, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(AlertDialogDemo.this,beautifulGirls[which]+"被选中啦",Toast.LENGTH_SHORT).show();
            }
        });
        //显示对话框
        builder.show();
    }
}

猜你喜欢

转载自www.linuxidc.com/Linux/2016-12/138654.htm
今日推荐