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

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

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

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

Q&A

解決済

2回答

2402閲覧

EditTextの使い方

teakiyoshi

総合スコア11

Android Studio

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

0グッド

0クリップ

投稿2019/05/05 03:02

AndrodStudioで複数のEditTextを使いたい

EditTextの使い方を教えて下さい 初心者ですよろしくお願いします。
5個又は6個のEditTextを配置してEditTextに入力した値をリアルタイムに個々のTextViewに表示したいのですが?
例えば
EditText1に入力した値をTextView1に表示
EditText2に入力した値をTextView2に表示
EditText3に入力した値をTextView3に表示
EditText5に入力した値をTextView4に表示
EditText5に入力した値をTextView5に表示
EditText6に入力した値をTextView6に表示と、

■■な機能を実装中に以下のエラーメッセージが発生しました。

発生している問題・エラーメッセージ

エラーメッセージ

該当のソースコード

AndroidStudio

1public class MainActivity extends Activity implements TextWatcher { 2 3@Override 4 public void onCreate(Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 setContentView(R.layout.activity_main); 7 8   EditText edittext = (EditText)findViewById(R.id.edittext); 9 edittext.addTextChangedListener(this); //追加 10 } 11 12 /*--- 追加 ---*/ 13 14 @Override 15 public void beforeTextChanged(CharSequence s, int start, int count,int after) { 16 //操作前のEtidTextの状態を取得する 17 } 18 19 @Override 20 public void onTextChanged(CharSequence s, int start, int before, int count) { 21 //操作中のEtidTextの状態を取得する 22 } 23 24 @Override 25 public void afterTextChanged(Editable s) { 26    //操作後のEtidTextの状態を取得する 27 28  /*--- 取得例(EditTextの更新内容をTextViewに反映) ---*/ 29    TextView textView = (TextView)findViewById(R.id.textview); 30   textView.setText(s.toString()); 31  /*--- 取得例 ---*/ 32 33 } 34 35 /*--- 追加 ---*/ 36 37} 38 39 40 41### 試したこと 42 43リアルタイムにTextViewに表示されますが 44複数の EditTextとTextViewを使うにはどうすればいいのでしょうか? 45 46### 補足情報(androidStudio2.3) 47 48ここにより詳細な情報を記載してください。

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

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

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

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

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

keicha_hrs

2019/05/05 04:10

コードの範囲は```の行で上下をサンドイッチするようにしてください。また、言語のところにAndroidStudioと記されていますが、AndroidStudioは言語ではなく開発環境(IDE)です。この場合はjavaと記述されるべきです。
teakiyoshi

2019/05/05 05:19

ご指摘有難うございます。次回から気を付けます。
keicha_hrs

2019/05/05 05:35

質問は自分で編集できますから、今からでも修正できますよ。
jimbe

2019/05/05 08:12

activity_main.xml をご質問に 追記 願えますでしょうか. その際は keicha_hrs さんご指摘のように ``` でサンドイッチして, 言語として xml とお願い致します.
teakiyoshi

2019/05/05 08:47

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.ith.ith_edit.MainActivity"> <EditText android:id="@+id/editText" android:layout_width="92dp" android:layout_height="41dp" android:layout_marginBottom="8dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginTop="8dp" android:ems="10" android:inputType="numberDecimal" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.072" /> <EditText android:id="@+id/editText2" android:layout_width="92dp" android:layout_height="41dp" android:layout_marginBottom="8dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginTop="8dp" android:ems="10" android:inputType="numberDecimal" android:selectAllOnFocus="false" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.22" /> <TextView android:id="@+id/textView" android:layout_width="79dp" android:layout_height="29dp" android:layout_marginBottom="8dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginTop="8dp" android:text="TextView" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintHorizontal_bias="0.501" app:layout_constraintVertical_bias="0.354" /> <TextView android:id="@+id/textView2" android:layout_width="79dp" android:layout_height="29dp" android:layout_marginBottom="8dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginTop="8dp" android:text="TextView" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.467" app:layout_constraintHorizontal_bias="0.501" /> </android.support.constraint.ConstraintLayout> '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' とりあえずedittext 2個、textview 2個です。宜しくお願いします
jimbe

2019/05/05 09:05

追記出来なかったようですね… ご提示されているコードと頂いたXMLの組み合わせではコンパイルエラーになりますが, 合っていますでしょうか.
teakiyoshi

2019/05/05 09:42

’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ /*--- 取得例(EditTextの更新内容をTextViewに反映) ---*/ TextView textView = (TextView)findViewById(R.id.textview); textView.setText(s.toString()); /*--- 取得例 ---*/ ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ 1個目のedittextの値を1個目textviewに値を表示できます。 teratailの使い方が不慣れなご迷惑をおかけします
guest

回答2

0

ベストアンサー

こんな感じでしょうか.

java

1public class MainActivity extends Activity { 2 @Override 3 public void onCreate(Bundle savedInstanceState) { 4 super.onCreate(savedInstanceState); 5 setContentView(R.layout.activity_main); 6 7 EditText edittext = (EditText)findViewById(R.id.editText); 8 edittext.addTextChangedListener(new TextForwarder((TextView)findViewById(R.id.textView))); 9 10 EditText edittext2 = (EditText)findViewById(R.id.editText2); 11 edittext2.addTextChangedListener(new TextForwarder((TextView)findViewById(R.id.textView2))); 12 } 13 14 private class TextForwarder implements TextWatcher { 15 private TextView textView; 16 TextForwarder(TextView textView) { 17 this.textView = textView; 18 } 19 //操作前のEditTextの状態を取得する 20 @Override 21 public void beforeTextChanged(CharSequence s, int start, int count,int after) {} 22 //操作中のEditTextの状態を取得する 23 @Override 24 public void onTextChanged(CharSequence s, int start, int before, int count) {} 25 //操作後のEditTextの状態を取得する 26 @Override 27 public void afterTextChanged(Editable s) { 28 textView.setText(s.toString()); 29 } 30 } 31}

投稿2019/05/05 09:16

jimbe

総合スコア12646

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

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

teakiyoshi

2019/05/05 09:59

できました。有難うございます。 コードを参考にして目指すものを作ってみます。 本当に有難うございます。
teakiyoshi

2019/05/07 10:06

jimbe様、ここでの質問可能しょうか?
jimbe

2019/05/07 10:08

回答に対するご質問でしたらもちろんです.
teakiyoshi

2019/05/07 10:18

質問する内容を上手くまとめることができませんが 下記のコードで演算処理をしたいのですが、    '''''''''''''''''''''''''''''''''''''''''''''' 中略 EditText edittext = (EditText)findViewById(R.id.editText); //下記の処理をして //String edt1= edittext.getText().toString(); // int a = Integer.parseInt(edt1); // int b = a+1000; //int i = Integer.parseInt(edt1); // 計算処理する   //計算した結果を最下位行の //@Override // public void afterTextChanged(Editable s) {textView.setText(s.toString()); // } に結果を渡すことができますか? //String edt1= edittext.getText().toString(); //int i = Integer.parseInt(edt1); edittext.addTextChangedListener(new TextForwarder((TextView)findViewById(textView))); EditText edittext2 = (EditText)findViewById(R.id.editText2); edittext2.addTextChangedListener(new TextForwarder((TextView)findViewById(R.id.textView2))); EditText edittext3 = (EditText)findViewById(R.id.editText3); edittext3.addTextChangedListener(new TextForwarder((TextView)findViewById(R.id.textView3))); } private class TextForwarder implements TextWatcher { private TextView textView; TextForwarder(TextView textView) { this.textView = textView; } //操作前のEditTextの状態を取得する @Override public void beforeTextChanged(CharSequence s, int start, int count,int after) {} //操作中のEditTextの状態を取得する @Override public void onTextChanged(CharSequence s, int start, int before, int count) {} //操作後のEditTextの状態を取得する @Override public void afterTextChanged(Editable s) { textView.setText(s.toString()); } //個々Textvewに表示 } } ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’
jimbe

2019/05/07 10:27

TextForwarder の動作をご理解頂いていますでしょうか. TextForwarder は, 自身を登録した EditText のデータを afterTextChanged で受け取り, 対応する(コンストラクタで指定された) TextView に書き込む動作をしています. EditText の内容を使って計算をし, 対応する TextView に書き込むのでしたら, afterTextChanged 内で行えます.
teakiyoshi

2019/05/07 10:39

有難うございます。試してみます
guest

0

試していませんが、
afterTextChanged(Editable s) の hashCodeとEtidTextのhashCodeを使うことで、どのEtidTextに入力されたかわかりそうです。

参考:https://codeday.me/jp/qa/20181208/71534.html

投稿2019/05/05 03:23

Akashic

総合スコア298

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

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

teakiyoshi

2019/05/07 10:24

回答有難うございます。参考にさせて貰いました
Akashic

2019/05/07 10:48

コメントありがとうございます。 teakiyoshiさんのコードに近い方が理解されやすいかと考え先のページをコメントさせて頂きました。が、jimbeさんのアンサーのように、onCreate()で Listener設定やfindViewByIdを行うコードが一般的(というか、綺麗に書ける)印象ですー。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問