只有选择单选按钮后,使编辑框 [英] enable edit box only after selecting the radio button
问题描述
我有两个单选按钮,即电话和电子邮件。当选择呼叫,然后在编辑框中应该得到启用,然后用户可以输入手机号码。
我检查这些链接:
<一href=\"http://stackoverflow.com/questions/21752408/enable-a-text-box-only-when-1-of-the-radio-button-is-clicked\">Enable只有一个文本框时,单选按钮1被点击
<一个href=\"http://stackoverflow.com/questions/10131827/enable-text-box-based-on-radio-button-selected\">Enable根据单选按钮选择文本框
但我不能够实现这一任务。
下面是我的code:
&LT;的LinearLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginRight =10dp
机器人:layout_marginLeft =10dp
机器人:layout_marginTop =10dp&GT; &LT; RadioGroup中
机器人:ID =@ + ID / radioGroup1
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:weightSum =2
机器人:方向=横向&GT; &LT;单选
机器人:ID =@ + ID / radio0
机器人:layout_width =0dp
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =1
机器人:文字=@字符串/通话/&GT; &LT;单选
机器人:ID =@ + ID /收音机1
机器人:layout_width =0dp
机器人:layout_height =WRAP_CONTENT
机器人:文字=@字符串/邮件
机器人:layout_weight =1/&GT;
&LT; / RadioGroup中&GT;
&LT; / LinearLayout中&GT; &LT;的LinearLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginRight =10dp
机器人:layout_marginLeft =10dp
机器人:layout_marginTop =10dp
机器人:weightSum =1&GT; &LT;的TextView
机器人:ID =@ + ID / textView21
机器人:layout_width =0dp
机器人:layout_height =WRAP_CONTENT
机器人:文字=@字符串/移动
机器人:文字颜色=#000000
机器人:TEXTSIZE =15sp
机器人:layout_weight =0.3/&GT; &LT;的EditText
机器人:ID =@ + ID / editText1
机器人:layout_width =0dp
机器人:layout_height =WRAP_CONTENT
机器人:EMS =10
安卓的inputType =numberDecimal
机器人:最大长度=10
机器人:layout_weight =0.7
安卓的inputType =无/&GT;
&LT; / LinearLayout中&GT;
的.java
公共类MainActivity延伸活动{
字符串移动;RadioGroup中皂苷Rg1 =(RadioGroup中)findViewById(R.id.radioGroup1);
编辑的EditText =(EditText上)findViewById(R.id.editText1);保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); edit.setEnabled(假);
edit.setInputType(InputType.TYPE_NULL);
edit.setFocusable(假); rg1.setOnCheckedChangeListener(新OnCheckedChangeListener(){ @覆盖
公共无效onCheckedChanged(RadioGroup中组,诠释checkedId){
开关(checkedId){
案例R.id.radio0:
OP1 =呼;
ACTV();
打破; 案例R.id.radio1:
OP1 =电子邮件;
打破; } }
}); 公共无效ACTV(){
edit.setEnabled(真);
edit.setInputType(InputType.TYPE_CLASS_TEXT);
edit.setFocusable(真);
}
除了纠正你的成员初始化(在每个previous答案的建议),你需要修复你的布局(实在是太封闭早,导致在无效的xml)。
&LT; LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直&GT; &LT; RadioGroup中
机器人:ID =@ + ID / radioGroup1
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginLeft =10dp
机器人:layout_marginRight =10dp
机器人:layout_marginTop =10dp
机器人:方向=横向
机器人:weightSum =2&GT; &LT;单选
机器人:ID =@ + ID / radio0
机器人:layout_width =0dp
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =1
机器人:文字=@字符串/通话/&GT; &LT;单选
机器人:ID =@ + ID /收音机1
机器人:layout_width =0dp
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =1
安卓:检查=真
机器人:文字=@字符串/邮件/&GT;
&LT; / RadioGroup中&GT; &LT;的LinearLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginLeft =10dp
机器人:layout_marginRight =10dp
机器人:layout_marginTop =10dp
机器人:weightSum =1&GT; &LT;的TextView
机器人:ID =@ + ID / textView21
机器人:layout_width =0dp
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =0.3
机器人:文字=@字符串/移动
机器人:文字颜色=#000000
机器人:TEXTSIZE =15sp/&GT; &LT;的EditText
机器人:ID =@ + ID / editText1
机器人:layout_width =0dp
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =0.7
机器人:EMS =10
机器人:最大长度=10/&GT;
&LT; / LinearLayout中&GT;&LT; / LinearLayout中&GT;
也决定你想prevent打字到的EditText(禁用/隐藏/输入型调整)用户哪种方式。
和最后但并非最不重要的,把所有的这些结合在一起以清洁的方式:
公共类MainActivity扩展活动实现OnCheckedChangeListener
{
私人字符串OP1; 私人RadioGroup中皂苷Rg1;
私人的EditText编辑; 保护无效的onCreate(捆绑savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); 皂苷Rg1 =(RadioGroup中)findViewById(R.id.radioGroup1);
rg1.setOnCheckedChangeListener(本); 编辑=(EditText上)findViewById(R.id.editText1);
ACTV(假);
} @覆盖
公共无效onCheckedChanged(RadioGroup中组,诠释checkedId)
{
开关(checkedId)
{
案例R.id.radio0:
OP1 =呼;
ACTV(真);
打破; 案例R.id.radio1:
OP1 =电子邮件;
ACTV(假);
打破;
}
} 私人无效ACTV(最终活跃布尔)
{
edit.setEnabled(活动);
如果(有源)
{
edit.requestFocus();
}
}
}
I have two radio buttons i.e., call and email. When call is selected then the edit box should get enabled and then user may enter the mobile number. I've checked these links: Enable a text box only when 1 of the radio button is clicked
Enable text box based on radio button selected
but am not able to achieve this task.
Here is my code:
<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);
}
Besides correcting your member initialization (as suggested in each of the previous answers), you need to fix your layout (it is closed too early, resulting in an invalid 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>
Also decide which way you'd like to prevent the user from typing into the edittext (disable / hide / input type tweaks).
And last but not least, put all of these together in a clean manner:
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();
}
}
}
这篇关于只有选择单选按钮后,使编辑框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!