Android通知之单选对话框通知

来源:互联网 发布:azul by moussy淘宝 编辑:程序博客网 时间:2024/06/10 01:16
int selectedIndex = -1;//单选通知public void radio(View view){OnClickListener choiceLinstener = new OnClickListener() {//这个是监听itempublic void onClick(DialogInterface dialog, int which) {//which是被选中item的索引selectedIndex = which;//用全局变量记录下来权重的item索引,供按钮监听方法调用并显示}};OnClickListener positiveLinstener = new OnClickListener() {//这个是监听按钮的public void onClick(DialogInterface dialog, int which) {//which是用来区分按钮的,跟普通通知那三个按钮一样//根据selectedIndex是否从-1改变为其他值判断是否选中值,因为全局变量selectedIndex初始值为-1Toast.makeText(getApplicationContext(), selectedIndex==-1?"没有选中":getResources().getStringArray(R.array.items)[selectedIndex], Toast.LENGTH_SHORT).show();selectedIndex = -1;}};new AlertDialog.Builder(this)//.setTitle("单选对话框")//.setCancelable(true)//这个表示点击手机上的返回键是否能取消掉.setSingleChoiceItems(R.array.items,-1, choiceLinstener)//第一个参数写items.setPositiveButton("确定", positiveLinstener)//.show();}

string.xml

<string-array name="items">    <item >魔兽世界wow</item>    <item >热血传奇</item>    <item >跑跑卡丁车</item></string-array>


0 0
原创粉丝点击