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

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

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

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

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

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

Q&A

1回答

1550閲覧

EditTextの入力がエンターを押すまで反映されない

makotonisio

総合スコア4

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

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

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

0グッド

0クリップ

投稿2020/03/25 07:33

編集2020/03/25 12:24

前提・実現したいこと

EditTextの入力欄がエンターを押すまで変化しないのを直したい

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

フラグメントに設置したEditTextの入力欄が文字を入力しても変化しません。
エンターキー押す、フォーカスを外すことで入力した文字は表示されます。
EditTextのChangeListenerに他のViewへの反映について記入したのですが、これは入力中は一部だけしか動かず、エンターを押すことでやっと全て反映されます。
LinearLayoutに複数のEditTextを入れているのですが、一番上のEditTextは正常に働きます。
またAPI25で実施しても問題なく動作します。

該当のソースコード

<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:id="@+id/mainLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".hero.MakingHero.MakedHeroA"> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="0.15" android:orientation="horizontal"> <Button android:id="@+id/samCount" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="0.2"/> </LinearLayout> <ImageButton android:id="@+id/heroImage" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/holo_orange_light" android:focusable="true" android:focusableInTouchMode="true" android:src="@drawable/black_bow_front_circle" /> <LinearLayout android:id="@+id/stetusLayout" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="0.5" android:baselineAligned="false" android:orientation="horizontal"> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="0.6" android:orientation="vertical" android:visibility="visible"> <TextView android:id="@+id/hp" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/> <TextView android:id="@+id/power" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/> <TextView android:id="@+id/def" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:focusable="true" android:focusableInTouchMode="true"/> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="0.2" android:orientation="vertical"> <EditText android:id="@+id/plusHp" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="bottom|center" android:hint="@string/count0_50" android:inputType="number" android:lines="1" android:maxLength="2" android:nextFocusDown="@+id/def" android:nextFocusForward="@+id/def" /> <EditText android:id="@+id/plusPow" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="bottom|center" android:hint="@string/count0_50" android:inputType="number" android:lines="1" android:maxLength="2" android:nextFocusDown="@+id/def" android:nextFocusForward="@+id/def" /> </LinearLayout> </LinearLayout> </LinearLayout>
package com.example.word_battle.hero.MakingHero import android.content.Context import android.content.Intent import android.net.Uri import android.os.Bundle import android.text.Editable import androidx.fragment.app.Fragment import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.text.TextWatcher import android.view.inputmethod.InputMethodManager import android.widget.EditText class MakedHeroA: Fragment(),TextWatcher { val textStetus = "%3s:%03d..+:%02d" fun sumCoun(sum:Int,count:Int):Int{ var sumX :Int = sum.minus(count) samCount.text="$sumX" return sumX } fun hpManage(A:Hero){ A.heroHp = A.hp+ A.hpCount hp.text = textStetus.format(" HP", A.heroHp, A.hpCount) hp.invalidate()} fun powManage(A:Hero){ A.heroPower = A.power+ A.powerCount power.text = textStetus.format("ATK", A.heroPower, A.powerCount)} fun heroMake(A: Hero, sum:Int, context:Context){ editForcus() heroX.name=A.name plusHp.setText(A.hpCount.toString()) plusPow.setText(A.powerCount.toString()) var hpCount=A.hpCount var powCount=A.powerCount var sumX = sum sumCoun(sumX,0) hpManage(A) powManage(A) //HP plusHp.addTextChangedListener(object :TextWatcher{//入力中の問題が起きない。 override fun afterTextChanged(s: Editable?) { val edit =s.toString() if(heroX.name!=A.name){return} if(!edit.equals("")&&edit.toInt() in 0..50){ val sa:Int = edit.toInt() - hpCount if(sumX-sa in 0..300){ sumX = sumCoun(sumX,sa) A.hpCount=edit.toInt() hpManage(A) hpCount=A.hpCount} } } }) //Power plusPow.addTextChangedListener(object :TextWatcher{//入力中の問題が起き、 viewもエンターを押さないと反映されない。 override fun afterTextChanged(s: Editable?) { val edit =s.toString() if(heroX.name!=A.name){return} if(!edit.equals("")&&edit.toInt() in 0..50){ val sa:Int = edit.toInt() - powCount if(sumX-sa in 0..300){ sumX = sumCoun(sumX,sa) A.powerCount=edit.toInt() powManage(A) powCount=A.powerCount} } ) } fun editForcus(){ val list= listOf<EditText>(plusHp,plusPow) list.forEach{ it.setOnFocusChangeListener { v, hasFocus -> if (!hasFocus) { val imm = activity?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager imm.hideSoftInputFromWindow(v.windowToken, InputMethodManager.HIDE_NOT_ALWAYS) } }} heroImage?.setOnTouchListener { v, event -> heroImage?.requestFocus() mainLayout?.onTouchEvent(event) ?: true } } }
package com.example.word_battle.online class HeroMakerModeOnline: AppCompatActivity(){ private var A: Hero = Hero() private var B: Hero = Hero() private var sum: Int = 0 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_hero_maker_mode) val fragment = (maker as? MakedHeroA)!! listButton = mutableListOf(button1,button2) button1.setOnClickListener { fragment?.heroMake(A, sum, this) } button2.setOnClickListener { fragment?.heroMake(B, sum, this) }} override fun onTouchEvent(event: MotionEvent?): Boolean { .requestFocus() mainlayout.requestFocus() return super.onTouchEvent(event) } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:id="@+id/mainlayout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:focusable="true" android:focusableInTouchMode="true" tools:context= ".hero.MakingHero.HeroMakerModeWhite"> <LinearLayout android:id="@+id/higherLayout" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="0" android:orientation="horizontal" /> <LinearLayout android:id="@+id/centerLayout" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="0.08" android:orientation="horizontal"> <ImageButton android:id="@+id/button1" style="@android:style/Widget.Button" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> <ImageButton android:id="@+id/button2" style="@android:style/Widget.Button" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1"/> </LinearLayout> <fragment android:id="@+id/maker" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="0.82" android:orientation="horizontal" android:name="com.example.word_battle.hero.MakingHero.MakedHeroA" tools:layout="@layout/fragment_maked_hero" /> </LinearLayout>

補足情報

androidstudio3.6.1
エミュレーター:ターゲットAndroid9.0,API28
よろしくお願いします。

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

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

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

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

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

jimbe

2020/03/25 10:02

コードは出来るだけ, 現象を再現するに必要な全てをご提示ください. その際はマークダウンによってファイル別にしてください.
jimbe

2020/03/25 12:06

どうも sumX の計算・判定の個所の仕様が分かりません. 50 というのは一体なんの数値なのでしょう. hp と power の加算数の合計の上限値(「hp を +15 にしたら power は +35 まで」等)でしょうか.
makotonisio

2020/03/25 12:36

申し訳ありません。マークダウンで編集したのですが、文字数制限のため全てを書くことはできませんでした。 sumXについてですが、今回のクラスはキャラクターにステータスを振るための機能のもので、sumXはそのステータスに振れる値です。hpに1振ると、sumXが1減るという形です。 50という数値は、各ステータスに振れる最大値です。最大50までしか数値を振れませんが、hp、powerに50ずつ振ることはできます。 ですのでsumXはステ振りが0..50までの間でしか動かないようにしていて、計算する時も既に振られている数値分との差を出す必要があります。
jimbe

2020/03/25 12:45

編集ありがとうございます. 文字数制限は仕方ありませんね. sumX 了解しました. もうひとつ, TextWatcher 内で if(sumX-sa in 0..300){ というコードがありますが, この 300 は各ステータスの最大値ということでしょうか.
makotonisio

2020/03/25 12:59

説明不足で申し訳ありません。 このフラグメントは、heroMake()使用して複数のキャラクターのステータスを管理しています。 アクティビティーからボタンを押すことでキャラクターの切り替えを行っています。 そして、300という数字はsumXの最大値です。 sumXは一人のキャラではなく、全体のキャラクターで共有しています。 例えば、AのHpに50、Bのpowerに40のステータスに振った時、sumXは210になります。 ですので、in 0..300というのは、sumXの下限と上限を表しています。
jimbe

2020/03/25 13:45

なるほど々々, ありがとうございます. それで, ご提示のコードから(パッケージが良く分からないので一つに纏めましたが)組み立ててみると, フラグメントが表示されません. そちらではこのコード,XMLで表示されているのでしょうか.
jimbe

2020/03/25 13:50

すいません出ました. ImageButton の src がテキトウなのが無かったので属性だけ消したら, witdh と height が match_parent で全体を覆っていました.
guest

回答1

0

ご提示のコードにコンパイルエラーがありましたので, それらを修正して動かしてみましたが, 仰るような状況になりませんで, hp/power とも, ENTERをせずとも数字を入力・変更するだけで表示に反映されました.

エミュレータ API 28 (Nexus 5)です.

修正したのは,
・パッケージを com.teratail.q249330 化.
・MakedHeroA.kt, TextWatcher インプリメント削除
・MakedHeroA.kt, onCreateView 追加

kotlin

1 override fun onCreateView(inflater: LayoutInflater, 2 container: ViewGroup?, savedInstanceState: Bundle?): View? { 3 super.onCreateView(inflater, container, savedInstanceState) 4 return inflater.inflate(R.layout.fragment_maked_hero, container, false) 5 }

・MakedHeroA.kt, heroX が見つからないためコメント化 4 か所.
・MakedHeroA.kt, plusHp/plusPow への addTextChangedListener の TextWatcher オブジェクトに定義が不足する 2 メソッドの追加(無処理)
・MakedHeroA.kt, editForcus メソッド前辺りの括弧の異常の修正
・HeroMakerModeOnline.kt, sum の初期値を 300
・fragment_maked_hero.xml,activity_hero_maker_mode.xmlとも, パッケージ変更
・fragment_maked_hero.xml, @+id/heroImage の src 属性削除, width=100dp, height=50dp (テキトウ)

などです.
ロジックや xml の動作には影響していないと思いますが...
修正途中をご提示されたようなので, その辺りで何か修正されてしまった(?)のかもしれません.

投稿2020/03/25 14:30

jimbe

総合スコア12648

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

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

makotonisio

2020/03/26 00:12

回答、ありがとうございます。 文字を削減しすぎたかもしれません。 何か別の方法でコードを全部載せられないか探してみます。 不十分なコードで申し訳ありませんでした。
jimbe

2020/03/26 02:11

バージョン管理システムで公開にも使われる [GitHub]( https://github.co.jp/ ) は如何でしょうか.
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問