标签:
因为项目中需要在多个测量选项中选择一项进行数据查询,所以首先是找找有什么控件是这类型的。很容易找到Spinner这一控件,但事实上做出来后,在AVD上运行感觉还可以但在实际手机上运行确实很丑,大抵是功夫不到家。所以试着使用疯狂讲义讲到的AlerDialog创建单选列表对话框,编程中遇到了一个问题。根据讲义写了一下代码,监听时出现了.setSingleChoiceItems ,OnClickListener() 都标红报错
1 public class Measure extends Activity 2 { 3 4 /*创建对话框选择需要测量的项目进行查询 */ 5 final int SINGLE_DIALOG=0x113; 6 @Override 7 protected void onCreate(Bundle savedInstanceState) 8 { 9 super.onCreate(savedInstanceState);//设置使用main.xml文件定义的界面布局 10 setContentView(R.layout.measure); 11 Button bn=(Button)findViewById(R.id.bn19);//获取UI界面中id为R.id.bn19的按钮 12 bn.setOnClickListener(new OnClickListener() 13 { 14 15 16 @SuppressWarnings("deprecation") 17 public void onClick(View source) 18 { 19 showDialog(SINGLE_DIALOG); 20 21 22 } 23 }); 24 } 25 @Override 26 public Dialog onCreateDialog(int id,Bundle state) 27 { 28 switch (id) 29 { 30 case SINGLE_DIALOG: 31 32 Builder b =new AlertDialog.Builder(this); 33 b.setIcon(R.drawable.abc_ab_share_pack_holo_dark); 34 b.setTitle("项目列表"); 35 b.setSingleChoiceItems(new String [] 36 {"发电相电压","发电线电压","发电负载电流","发电频率","发电功率因数","发电视在功率","发电有功功率","发电无功功率","转速油压水温等"} 37 ,0 38 ,new OnClickListener() 39 { 40 41 public void onClick(DialogInterface dialog,int which) 42 { 43 // TODO Auto-generated method stub 44 TextView show1 =(TextView)findViewById(R.id.View6); 45 switch (which) 46 { 47 case 0: 48 show1.setText("发电相电压"); 49 Setgu4(which); 50 Setgu7(which); 51 Setgu9(which); 52 Setgu10(which); 53 Setgu12(which); 54 break; 55 case 1: 56 ...... 57 58 } 59 } 60 61 }); 62 63 b.setPositiveButton("确定",null); 64 return b.create(); 65 } 66 return null; 67 }
于是在网上看了看,监听器是需要是需要加上DialogInterface.才行。直接写new OnClickListener(),程序应该会直接找到View.onClickListener,不满足这个监听的要求,所以得指定是DialogInterface里面的onClickListener,这样就不会出现问题了
1 b.setSingleChoiceItems(new String [] 2 {"发电相电压","发电线电压","发电负载电流","发电频率","发电功率因数","发电视在功率","发电有功功率","发电无功功率","转速油压水温等"} 3 ,0 4 ,new DialogInterface.OnClickListener()
标签:
原文地址:http://www.cnblogs.com/yongdaiblog-201409/p/4334099.html