###前提・実現したいこと
RadioButtonで「入力」の方を選択すると、Edittextが入力できるようにしたいです。
選択していない場合は、入力できないようにしたいです
###該当のソースコード
fragment_main.xml
xml
1<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:app="http://schemas.android.com/apk/res-auto" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent"> 6<LinearLayout 7 android:layout_width="match_parent" 8 android:layout_height="wrap_content" 9 android:orientation="horizontal"> 10 11 <RadioGroup 12 android:id="@+id/RadioGroup" 13 android:layout_width="match_parent" 14 android:layout_height="wrap_content"> 15 16 <RadioButton 17 android:id="@+id/rbtn_test" 18 android:layout_width="match_parent" 19 android:layout_height="wrap_content" 20 android:text="あいうえお"></RadioButton> 21 22 <RadioButton 23 android:id="@+id/rbtn_other" 24 android:layout_width="match_parent" 25 android:layout_height="wrap_content" 26 android:text="その他(入力してください)"></RadioButton> 27 28 <EditText 29 android:id="@+id/txt_other" 30 android:layout_width="fill_parent" 31 android:layout_height="wrap_content" 32 android:enabled="false" 33 android:focusable="false" 34 android:inputType="text" /> 35 </RadioGroup> 36 </LinearLayout> 37</FrameLayout>
main.java
Java
1 @Override 2 public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 3 final View v = inflater.inflate(R.layout.fragment_upload,container, false); 4 5 RadioGroup radioGroup = (RadioGroup)v.findViewById(R.id.RadioGroup); 6 radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 7 @Override 8 public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) { 9 if (checkedId != -1){ 10 RadioButton radioButton = (RadioButton)v.findViewById(checkedId); 11 if (radioButton == rbtn_other){ 12 Toast.makeText(getActivity(), 13 "入力してください", Toast.LENGTH_SHORT).show(); 14 txt_other.setFocusable(true); 15 txt_other.setFocusableInTouchMode(true); 16 txt_other.setEnabled(true); 17 txt_other.requestFocus(); 18 } 19 else { 20 } 21 } 22 } 23 }); 24return v;
###解決後
fragment_main.xml
xml
1<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:app="http://schemas.android.com/apk/res-auto" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent"> 6<LinearLayout 7 android:layout_width="match_parent" 8 android:layout_height="wrap_content" 9 android:orientation="horizontal"> 10 11 <RadioGroup 12 android:id="@+id/RadioGroup" 13 android:layout_width="match_parent" 14 android:layout_height="wrap_content"> 15 16 <RadioButton 17 android:id="@+id/rbtn_test" 18 android:layout_width="match_parent" 19 android:layout_height="wrap_content" 20 android:text="あいうえお"></RadioButton> 21 22 <RadioButton 23 android:id="@+id/rbtn_other" 24 android:layout_width="match_parent" 25 android:layout_height="wrap_content" 26 android:text="その他(入力してください)"></RadioButton> 27 28 <EditText 29 android:id="@+id/txt_other" 30 android:layout_width="fill_parent" 31 android:layout_height="wrap_content" 32 android:visibility="gone" 33 android:inputType="text" /> 34 </RadioGroup> 35 </LinearLayout> 36</FrameLayout>
main.java
Java
1 @Override 2 public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 3 final View v = inflater.inflate(R.layout.fragment_upload,container, false); 4 5 rbtn_other = (RadioButton) v.findViewById(R.id.rbtn_other); 6 7 rbtn_other.setOnClickListener(new View.OnClickListener() { 8 @Override 9 public void onClick(View v) { 10 EditText txt_other = (EditText)getActivity().findViewById(R.id.txt_other); 11 if (txt_other.getVisibility() != View.VISIBLE){ 12 txt_other.setVisibility(View.VISIBLE); 13 }else { 14 txt_other.setVisibility(View.INVISIBLE); 15 } 16 } 17 }); 18return v;
###試したこと
XMLのelse部の
android:enabled="false"
android:focusable="false"
を削除して、Java側の else部に
txt_other.setFocusable(false);
txt_other.setFocusableInTouchMode(false); txt_other.setEnabled(false);
を入力してみましたが、こうするとEdittextが入力できるようになってしまいました。
###補足情報(言語/FW/ツール等のバージョンなど)
PC:Windows10
AndroidStudio:バージョン2.3
デバッグ環境
Genymotion:バージョン2.8.1
Custom Phone-5.0.0(API21)
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/03/10 05:24