質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Q&A

解決済

1回答

2765閲覧

RadioButtonでEdittextの入力をできるようにしたい

hiziki

総合スコア54

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

1グッド

0クリップ

投稿2017/03/09 15:29

編集2017/03/10 05:31

###前提・実現したいこと
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)

vitabrevisarsl1👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

何ができていて、何ができていないのかがわかりませんが、下記のようにコードをリファクタリングした方が良さそうです。
・checkedIdがR.id.rbtn_otherだったら入力可にする。
・上記以外なら入力不可にする。

投稿2017/03/09 17:54

yona

総合スコア18155

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hiziki

2017/03/10 05:24

ご回答ありがとうございました。シンプルにしてみました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問