【问题标题】:create edittext and radiobutton dynamically动态创建edittext和radiobutton
【发布时间】:2014-06-16 17:48:02
【问题描述】:

我怎样才能动态创建edittext和radiobutton,这样要么 我可以在edittext中写,也可以单击单选按钮,但我应该 不要两者都做,然后我选择或写下答案 我需要保存。

我正在搜索它,但我没有得到任何合适的答案,请帮助我!

RadioButton toggleRadioButton;
    RadioGroup editRadiogroup = new RadioGroup(this);
    editRadiogroup.setOrientation(RadioGroup.HORIZONTAL);
    LinearLayout  editlayout=new LinearLayout(this);
    editlayout.setOrientation(LinearLayout.HORIZONTAL); 
         try
      {
      for (int i = 0; i < answer.length; i++)
         {

    int id = Integer.parseInt(5+ "" + i);
    // add edit text
         EditText editText;
         editText = new EditText(this);
         editText.setMaxLines(1);
         editText.setGravity(Gravity.LEFT);
         editText.setInputType(InputType.TYPE_CLASS_NUMBER);
         editText.setId(id);
    editlayout.addView(editText, WrapWidth, WrapHeight);
     TextView tv = new TextView(this);
     tv.setTextColor(Color.BLACK);
     tv.setTextSize(18);
     tv.setText("Year" +" " +"(or)"+"  ");
     editlayout.addView(tv);

    // add radio button
    toggleRadioButton = new RadioButton(this);
    toggleRadioButton.setChecked(false);
    toggleRadioButton.setTextSize(15);
    toggleRadioButton.setLayoutParams(new       LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

editRadiogroup.addView(toggleRadioButton,WrapWidth1,WrapHeight);

【问题讨论】:

  • 您可以只写“你的年龄是多少?(可选)”,或者让 EditText 的提示说“可选”。如果他们没有输入任何文字,他们就拒绝回答。
  • 其实我可以在edittext中写下答案,也可以直接拒绝

标签: android android-edittext textview radio-button checkboxlist


【解决方案1】:

您好,您可以收听编辑文本的更改: How do listen EditText?

当您在编辑文本中写入文本时,您可以使用 toggleRadiobutton.setVisibilty(View.INVISIBLE) 使 toggleRadiobutton 消失,或者使用 toggleRadiobutton.setEnabled(false) 禁用它。

同样如果检测到edittext中没有文本,可以重新启用togglebutton。

使用 toggleRadioButton 您将采用相同的方法。您在 OnClickListener 上检查按钮的状态并相应地启用或禁用 Edittext:

toggleRadioButton.setOnClickListener(new OnClickListener(View v){
    if(((RadioButton)v).isChecked()){
       edittext.setEnabled(false);
    }else{
       edittext.setEnabled(true);
    }
});

【讨论】:

  • 谢谢胡安.....我做到了,而不是使用单选按钮,我使用复选框并且它正在工作。
猜你喜欢
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-03
  • 2011-07-22
相关资源
最近更新 更多