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);
}