【问题标题】:enable edit box only after selecting the radio button仅在选择单选按钮后启用编辑框
【发布时间】:2015-02-20 12:26:45
【问题描述】:

我有两个单选按钮,即呼叫和电子邮件。选择通话后,应启用编辑框,然后用户可以输入手机号码。 我检查了这些链接: Enable a text box only when 1 of the radio button is clicked

Enable text box based on radio button selected

Making Text box not editable

但我无法完成这项任务。

这是我的代码:

     <LinearLayout 
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:layout_marginRight="10dp"
     android:layout_marginLeft="10dp"
     android:layout_marginTop="10dp"  >

            <RadioGroup
            android:id="@+id/radioGroup1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="2" 
            android:orientation="horizontal">

             <RadioButton
             android:id="@+id/radio0"
             android:layout_width="0dp"
             android:layout_height="wrap_content" 
             android:layout_weight="1"
             android:text="@string/call"/>

             <RadioButton
             android:id="@+id/radio1"
             android:layout_width="0dp"
             android:layout_height="wrap_content"
             android:text="@string/email"
             android:layout_weight="1" />
             </RadioGroup>
             </LinearLayout>

    <LinearLayout 
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:layout_marginRight="10dp"
     android:layout_marginLeft="10dp"
     android:layout_marginTop="10dp"
     android:weightSum="1">

    <TextView
        android:id="@+id/textView21"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/mobile"
        android:textColor="#000000"
        android:textSize="15sp"
        android:layout_weight="0.3" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="numberDecimal"
        android:maxLength="10"
        android:layout_weight="0.7"
        android:inputType="none" />
    </LinearLayout> 

.java

public class MainActivity extends Activity {
String mobile;

RadioGroup rg1 = (RadioGroup) findViewById(R.id.radioGroup1);
EditText edit = (EditText) findViewById(R.id.editText1);

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    edit.setEnabled(false);
    edit.setInputType(InputType.TYPE_NULL);
    edit.setFocusable(false);

    rg1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId) {
            case R.id.radio0:   
                op1="call";
                actv(); 
                break;

            case R.id.radio1:
                op1="email";
                break;

            }   

        }
    });

    public void actv() {
    edit.setEnabled(true);
        edit.setInputType(InputType.TYPE_CLASS_TEXT);
        edit.setFocusable(true);
    }

【问题讨论】:

  • 你能定义不工作吗?这段代码会发生什么?
  • 你在java哪里定义rg1??
  • @Zoya RadioGroup rg1 = (RadioGroup) findViewById(R.id.radioGroup1);在 onCreate 方法@MagicalPhoenixϡ 之前包含此内容不工作意味着屏幕未显示..这是我的第一个屏幕,所以一旦我启动应用程序,它就会显示“应用程序已意外停止..请重试..”
  • 请发布你的日志
  • 对不起@zoya...我正在下载.apk 并测试..log cat 没有生成,这就是为什么我无法追踪错误

标签: android android-edittext radio-button


【解决方案1】:

除了更正您的成员初始化(如前面每个答案中所建议的那样),您还需要修复您的布局(它关闭得太早,导致 xml 无效)。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:orientation="horizontal"
        android:weightSum="2" >

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/call" />

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:checked="true"
            android:text="@string/email" />
    </RadioGroup>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:weightSum="1" >

        <TextView
            android:id="@+id/textView21"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.3"
            android:text="@string/mobile"
            android:textColor="#000000"
            android:textSize="15sp" />

        <EditText
            android:id="@+id/editText1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.7"
            android:ems="10"
            android:maxLength="10" />
    </LinearLayout>

</LinearLayout>

还要确定您希望阻止用户输入编辑文本的方式(禁用/隐藏/输入类型调整)。

最后但并非最不重要的一点,以简洁的方式将所有这些放在一起:

public class MainActivity extends Activity implements OnCheckedChangeListener
{
    private String op1;

    private RadioGroup rg1;
    private EditText edit;

    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rg1 = (RadioGroup) findViewById(R.id.radioGroup1);
        rg1.setOnCheckedChangeListener(this);

        edit = (EditText) findViewById(R.id.editText1);
        actv(false);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId)
    {
        switch (checkedId)
        {
            case R.id.radio0:
                op1 = "call";
                actv(true);
                break;

            case R.id.radio1:
                op1 = "email";
                actv(false);
                break;
        }
    }

    private void actv(final boolean active)
    {
        edit.setEnabled(active);
        if (active)
        {
            edit.requestFocus();
        }
    }
}

【讨论】:

  • @rekaszeru...1 疑问..我们可以在 android 的 java 页面中包含 php 代码吗(如果是,那么如何)..见,我想检索存储在我的本地主机数据库中的数据使用WHERE 子句(如 SELECT * FROM information WHERE mobile='9123456789'..)并在下一个表单中填写详细信息(类似于自动填充,但我不想在编辑文本中给出 ant 建议)..我如何实现它..我应该包括/导入一个php文件
【解决方案2】:

@Zoya RadioGroup rg1 = (RadioGroup) findViewById(R.id.radioGroup1); 在 onCreate 方法之前包含这个 @MagicalPhoenixϡ 不工作 表示屏幕未显示..这是我的第一个屏幕 我启动应用程序它说“应用程序已停止 出乎意料..请再试一次..”

它正在崩溃。

原因:

RadioGroup rg1 = (RadioGroup) findViewById(R.id.radioGroup1);
EditText edit = (EditText) findViewById(R.id.editText1);

你已经包含了这些之前 onCreate 方法。

这显然行不通。

onCreate 方法中的 setContentView 调用之后添加它。

希望对你有帮助。

【讨论】:

    【解决方案3】:

    这是你需要做的

    改变这个

    RadioGroup rg1 = (RadioGroup) findViewById(R.id.radioGroup1);
    EditText edit = (EditText) findViewById(R.id.editText1);
    
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    

    到这里

    RadioGroup rg1 = null;
    EditText edit = null;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rg1 = (RadioGroup) findViewById(R.id.radioGroup1);
        edit = (EditText) findViewById(R.id.editText1);
    }
    

    谢谢:)

    【讨论】:

      【解决方案4】:

      添加:

      rg1 = (RadioGroup) findViewById(R.id.radioGroup1);
      edit = (EditText) findViewById(R.id.editText1);
      

      之后:

      setContentView(R.layout.activity_main);
      

      在你的 onCreate();

      还要确保在选中 radio1 时设置 edit.setEditable(false)。

      完整代码:

      public class MainActivity extends Activity {
      String mobile;
      
      RadioGroup rg1 = null;
      EditText edit = null;
      
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
      
          rg1 = (RadioGroup) findViewById(R.id.radioGroup1);
          edit = (EditText) findViewById(R.id.editText1);
          edit.setEnabled(false);
          edit.setInputType(InputType.TYPE_NULL);
          edit.setFocusable(false);
      
          rg1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
      
              @Override
              public void onCheckedChanged(RadioGroup group, int checkedId) {
                  switch (checkedId) {
                  case R.id.radio0:   
                      op1="call";
                      enableEdit(true); 
                      break;
      
                  case R.id.radio1:
                      op1="email";
                      enableEdit(false); 
                      break;
      
                  }   
      
              }
          });
          }
      
          public void enableEdit(boolean state) {
              edit.setEnabled(state);
              edit.setFocusable(state);
              if(state){ edit.setInputType(InputType.TYPE_CLASS_TEXT); } 
              else { edit.setInputType(InputType.TYPE_NULL); }
          }
      }
      

      【讨论】:

      • 当radio1被选中时设置edit.setEditable(false)...我应该在switch case中添加这个语句吗..我添加了1)edit.setEnabled(false); 2) 编辑.setInputType(InputType.TYPE_NULL); 3) 编辑.setFocusable(false); onCreate 方法中的这 3 行
      • 是的,您可以将其添加到 OnCheckedStateListener 中的开关语句中。检查“完整代码”。祝你好运!
      • 不...选择通话后编辑文本未启用):
      猜你喜欢
      • 1970-01-01
      • 2022-08-14
      • 1970-01-01
      • 2021-04-24
      • 1970-01-01
      • 1970-01-01
      • 2012-03-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多