2011-12-05 70 views
0

我在“button2.setOnClickListener(radio_listener)”上得到一个错误;“android如何动态改变基于radiogroups选择的radiogroup

因为它内嵌在onclick监听器中调用自己,所以最近怎么样?

final RadioGroup rGroup3 = (RadioGroup) findViewById(R.id.radio_gcam); 

    OnClickListener radio_listener = new OnClickListener() { 

     public void onClick(View v) { 
      RadioButton button = (RadioButton) v; 
      no_of_gcams = 3; 
      gcam_names_array[0] = "Machine 1"; 
      gcam_names_array[1] = "Machine 2"; 
      gcam_names_array[2] = "Machine 3"; 
      gcam_names_array[3] = "Machine 4"; 

      for (int i = 0; i < no_of_gcams; i++) 
      { 

      RadioButton button2 = new RadioButton(ModbusDroid.this); 
      button2.setText(gcam_names_array[i]); 
      button2.setOnClickListener(radio_listener);  
      rGroup3.addView(button2); 

      } 
     } 
    }; 

    if (no_of_gcams == 0) 
    { 
    RadioButton button1 = new RadioButton(this); 
    button1.setText("Setup a G-CAM"); 

    button1.setOnClickListener(radio_listener); 
    rGroup3.addView(button1);} 

回答

0

尝试下面的代码片段,它会帮助:

button2.setOnClickListener(this); 
+0

干杯,简单但有效:) – Monkins1010

0

嗨现在我有问题IVE移动的 “button2.setOnClickListener(本);”在别的地方,这不能被初始化,任何想法。?可能会取消选中复选框并在菜单中放置按钮以创建微粒选项。克里斯

final OnClickListener radio_listener = new OnClickListener() { 

     public void onClick(View v) { 
      RadioButton button = (RadioButton) v; 
      //Toast.makeText(getBaseContext(),button.getText(), 5).show(); 

      no_of_gcams = no_of_gcams+1; 


      LayoutInflater factory2 = LayoutInflater.from(ModbusDroid.this); 
      textEntryNumericView = factory2.inflate(R.layout.write_value_numeric, null); 
      AlertDialog.Builder writeNumericDialogBuilder = new 
       AlertDialog.Builder(ModbusDroid.this); 
      writeNumericDialogBuilder.setTitle("Write to Register") 
       .setView(textEntryNumericView) 
       .setIcon(R.drawable.ic_dialog_edit) 
       .setMessage("Value to Write to Register") 
       .setNegativeButton("Cancel", 
         new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         dialog.dismiss(); 
        } 
       }) 
       .setPositiveButton("Write", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       gcam_names_array[0] = ((EditText) textEntryNumericView.findViewById(R.id.write_value_number)).getText().toString(); 
       Toast.makeText(getBaseContext(), "Writing Value: " + gcam_names_array[0], 5).show(); 

       RadioButton button2 = new RadioButton(ModbusDroid.this); 
       button2.setText(gcam_names_array[0]); 
       button2.setId(no_of_gcams); 
       button2.setOnClickListener(this);  
       rGroup3.addView(button2); 
       dialog.dismiss(); 
      } 

     }); 

      writeNumericRegisterDialog = writeNumericDialogBuilder.create(); 
      writeNumericRegisterDialog = writeNumericDialogBuilder.show(); 

     } 
     }; 
相关问题