标签:water fonts == rate final dia data 回退 单选
效果图:
XML 文件:
创建一个点击button并加入点击方法:<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="showAlertDialog"
android:text="alertDialog" />
</LinearLayout>
public void showAlertDialog(View view){
/*
* 设置单选items
* */
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);//内部使用构建者的设计模式
final String[] items = {"苹果","橘子","香蕉"};
builder.setTitle("Dialog");
builder.setSingleChoiceItems(items, -1,new OnClickListener() {//第二个參数是设置默认选中哪一项-1代表默认都不选
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, items[which], 0).show();
}
});
builder.setCancelable(false);//设置dialog仅仅能通过点击Dialog上的button退出,不能通过回退button退出关闭Dialog
builder.create().show();//创建对象
}Android之在Dialog中加入单选button【自己定义Dialog】
标签:water fonts == rate final dia data 回退 单选
原文地址:http://www.cnblogs.com/ljbguanli/p/7076689.html