【发布时间】: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
但我无法完成这项任务。
这是我的代码:
<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