グーグルによって説明されているようにエラーメッセージを表示するようにAndroid EditTextを設計する


151

EditTextこのonErrorのようなものが必要です:

ここに画像の説明を入力してください

代わりにonErrorを呼び出すと、次のようになります。

ここに画像の説明を入力してください

注:アプリはSDK 19(4.4.2)で実行されています

最小SDKは1

これを自動的に行うsetErrorに似たメソッドはありますか、それともコードを記述する必要がありますか?

ありがとうございました


1
あなただけでそれを行うことができる可能性は低いEditTextです。より可能性が高いのは、ラップするEditTextか、追加する何かが必要になるでしょう。github.com/rengwuxian/MaterialEditTextを参照してください。
CommonsWare、2015年

回答:


330

GoogleがのTextInputLayout一部としてを導入したため、サードパーティのライブラリを使用する必要はありませんdesign-support-library

基本的な例に従ってください:

レイアウト

<android.support.design.widget.TextInputLayout
    android:id="@+id/text_input_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:errorEnabled="true">

    <android.support.design.widget.TextInputEditText
        android:id="@+id/edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter your name" />

</android.support.design.widget.TextInputLayout>

注:app:errorEnabled="true"属性として設定TextInputLayoutすると、エラーが表示されてもサイズは変更されません。そのため、基本的にスペースがブロックされます。

コード

の下にエラーを表示するには EditText、あなたは、単に呼び出す必要が#setErrorTextInputLayout(NOT子のEditText):

TextInputLayout til = (TextInputLayout) findViewById(R.id.text_input_layout);
til.setError("You need to enter a name");

結果

エラーメッセージを含む編集テキストを示す画像

エラーを非表示にして色合いをリセットするには、を呼び出しますtil.setError(null)


注意

を使用するTextInputLayoutには、build.gradle依存関係に以下を追加する必要があります。

dependencies {
    compile 'com.android.support:design:25.1.0'
}

カスタムカラーを設定する

デフォルトでは、ラインはEditText赤になります。別の色を表示する必要がある場合は、を呼び出したらすぐに次のコードを使用できますsetError

editText.getBackground().setColorFilter(getResources().getColor(R.color.red_500_primary), PorterDuff.Mode.SRC_ATOP);

クリアするにはclearColorFilter、次のように関数を呼び出します。

editText.getBackground().clearColorFilter();

3
はい !それでおしまい !どのようにして線を赤くしますか?
kmn

3
@kmn線を赤にすることは箱から出ることはないので、自分で行う必要があります。私の編集を参照してください。ヒントの移動に関しては、それは不可能です。
reVerse 2015年

5
@Alessio AppCompatActivityを拡張するときに機能するはずだと私は言ってきました。それにもかかわらず:appcompat-v23以降textInputLayout.setError("Error messsage")、の色を呼び出すとすぐにcolorFilterを設定する必要がなくなり、EditText赤に変わります。それをリセットするには、を呼び出すだけで十分textInputLayout.setError(null)です。
2015年

3
editText.getBackground().setColorFilter(getResources().getColor(R.color.red_500_primary), PorterDuff.Mode.SRC_ATOP);最新のサポートライブラリでは、もう必要ありません
Antoine Bolvy '23年

13
ほんの少し細かいので注意したいのですが、EditTextではなくでsetErrorを呼び出すことでこの問題が発生していましたTextInputLayout。私はこの答えを見ましたが、何を変える必要があるのか​​まだ分かりませんでした。見逃すのはとても簡単なこと。
h_k

8

myTextInputLayout.setError()代わりに呼び出しますmyEditText.setError()

これらのコンテナとコンテインメントには、設定エラーに関する二重の機能があります。必要な機能はコンテナの機能です。しかし、そのためには最低限23のバージョンが必要になる可能性があります。


7

あなたEditTextはに包まれるべきですTextInputLayout

<android.support.design.widget.TextInputLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/tilEmail">

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"
        android:ems="10"
        android:id="@+id/etEmail"
        android:hint="Email"
        android:layout_marginTop="10dp"
        />

</android.support.design.widget.TextInputLayout>

希望どおりのエラーメッセージを表示するには、errorを TextInputLayout

TextInputLayout tilEmail = (TextInputLayout) findViewById(R.id.tilEmail);
if (error){
    tilEmail.setError("Invalid email id");    
}

デザインサポートライブラリの依存関係を追加する必要があります。あなたのgradle依存関係にこの行を追加してください

compile 'com.android.support:design:22.2.0'

4

reVerseの答えは素晴らしいですが、フローティングエラーのツールチップの種類を削除する方法を指摘していませんでした

edittext.setError(null)削除する必要があります。
また、誰かが指摘したように、あなたは必要ありませんTextInputLayout.setErrorEnabled(true)

レイアウト

<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <EditText
        android:id="@+id/edittext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter something" />
</android.support.design.widget.TextInputLayout>

コード

TextInputLayout til = (TextInputLayout) editText.getParent();
til.setError("Your input is not valid...");
editText.setError(null);


0

誰かが答えで述べられているようにグーグルのデザインライブラリを使用してまだエラーに直面している場合は、@ h_kによってコメントされているようにこれを使用してください-

TextInputLayoutsetErrorを呼び出す代わりに、EditText自体でsetErrorを使用している可能性があります。


0
private EditText edt_firstName;
private String firstName;

edt_firstName = findViewById(R.id.edt_firstName);

private void validateData() {
 firstName = edt_firstName.getText().toString().trim();
 if (!firstName.isEmpty(){
//here api call for ....
}else{
if (firstName.isEmpty()) {
                edt_firstName.setError("Please Enter First Name");
                edt_firstName.requestFocus();
            } 
}
}

古い質問に回答するとき、回答がどのように役立つかを説明するコンテキストを含めれば、他のStackOverflowユーザーにとって、特に既に回答が受け入れられている質問に対して、回答がはるかに役立つでしょう。参照:良い回答を書くにはどうすればよいですか
David Buck
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.