3

私はレイアウトボタンを押してテキスト入力レイアウトにエラーが発生すると、editTextを赤色に変えますが、 基本的には、エラーの設定時にeditTextの下にエラー文字列を指定し、editTextを赤にしないだけです。 マイフラグメント・コードは次のように、親のような線形レイアウトで次のように断片ためTextInputレイアウトは、設定エラー時にeditextを赤色に変えます。

public class SignIn extends Fragment implements View.OnClickListener 
{ 
TextInputLayout edEamil,edPassword; 

@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
    super.onCreateView(inflater, container, savedInstanceState); 
    // LogUtil.e(TAG,"onCreateView"); 
    return inflater.inflate(R.layout.fragment_signin,container,false); 
} 

@Override 
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 
    // LogUtil.e(TAG,"onViewCreated"); 
    Button btSignin= GeneralFunction.findViewByIdAndCast(view, R.id.fs_bt_submit); 
    btSignin.setOnClickListener(this); 
    btSignin.setText(GeneralFunction.covertFistChat_everyWordUpper(getString(R.string.first_signin))); 

    TextView tvForgetPassword= GeneralFunction.findViewByIdAndCast(view, R.id.fs_tv_fPassword); 
    tvForgetPassword.setOnClickListener(this); 

    TextView tvSignUP= GeneralFunction.findViewByIdAndCast(view, R.id.fs_tv_fSignup); 
    tvSignUP.setOnClickListener(this); 
    tvSignUP.setText(GeneralFunction.covertFistChat_everyWordUpper(getString(R.string.first_signUP))); 

    edEamil=GeneralFunction.findViewByIdAndCast(view,R.id.fs_ti_email); 
    DrawableState.editextSelector(edEamil); 

    edPassword=GeneralFunction.findViewByIdAndCast(view,R.id.fs_ti_password); 
    DrawableState.editextSelector(edPassword); 
} 

@Override 
public void onClick(View v) { 
    switch (v.getId()) 
    { 
     case R.id.fs_bt_submit: 
     { 
      edEamil.setError("Not a valid email address!"); 
      edPassword.setError("Not a valid password!"); 
     }break; 
    } 
} 
} 

マイXMLコードは、ある

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<android.support.design.widget.TextInputLayout 
    android:id="@+id/fs_ti_email" 
    style="@style/textInputLayout"> 

    <EditText 
     style="@style/edittext" 
     android:tag="@string/str_email" 
     android:hint="@string/str_email"/> 
</android.support.design.widget.TextInputLayout> 

<android.support.v4.widget.Space 
    android:layout_width="match_parent" 
    android:layout_height="@dimen/dim_edMarginTop"/> 

<android.support.design.widget.TextInputLayout 
    android:id="@+id/fs_ti_password" 
    style="@style/textInputLayout"> 

    <EditText 
     style="@style/edittext" 
     android:imeOptions="actionDone" 
     android:inputType="textPassword" 
     android:tag="@string/str_password" 
     android:hint="@string/str_password"/> 
</android.support.design.widget.TextInputLayout> 

<android.support.v4.widget.Space 
    android:layout_width="match_parent" 
    android:layout_height="@dimen/dim_edMarginButtonView"/> 

<android.support.v7.widget.AppCompatButton style="@style/button" 
    android:id="@+id/fs_bt_submit" 
    android:text="@string/first_signin"/> 

<android.support.v4.widget.Space 
    android:layout_width="match_parent" 
    android:layout_height="@dimen/dim_edPaddingLeftRigh"/> 

<RelativeLayout android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
    <TextView style="@style/textView" 
     android:id="@+id/fs_tv_fPassword" 
     android:text="@string/str_forgetPassword" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentLeft="true"/> 
    <TextView style="@style/textView" 
     android:id="@+id/fs_tv_fSignup" 
     android:text="@string/first_signUP" 
     android:layout_alignParentEnd="true" 
     android:layout_alignParentRight="true"/> 
</RelativeLayout> 

とeditextと入力レイアウトのスタイルのようになります次のようになります。

<style name="textInputLayout"> 
    <item name="android:layout_width">match_parent</item> 
    <item name="android:layout_height">wrap_content</item> 
    <item name="android:paddingBottom">@dimen/dim_textLaoutMargin</item> 
    <item name="android:layout_marginBottom">@dimen/dim_textLaoutMargin</item> 
</style> 

<style name="edittext" parent="android:Widget.EditText"> 
    <item name="android:singleLine">true</item> 
    <item name="android:textSize">@dimen/dim_widgetMarginLeft</item> 
    <item name="android:textColor">@color/colorWhite</item> 
    <item name="android:paddingLeft">@dimen/dim_edPaddingLeftRigh</item> 
    <item name="android:paddingRight">@dimen/dim_edPaddingLeftRigh</item> 
    <item name="android:paddingTop">@dimen/dim_edPaddingTop</item> 
    <item name="android:paddingBottom">@dimen/dim_edPaddingBottom</item> 
    <item name="android:gravity">start</item> 
    <item name="android:background">@android:color/transparent</item> 
    <item name="android:textColorHint">@color/colorWhite</item> 
    <item name="android:layout_width">match_parent</item> 
    <item name="android:layout_height">@dimen/dim_buttonHeight</item> 
    <item name="android:inputType">text|textCapSentences|textNoSuggestions</item> 
    <item name="android:privateImeOptions">nm</item> 
    <item name="android:imeOptions">actionNext</item> 

    <item name="colorControlNormal">@color/colorWhite</item> 
    <item name="colorControlActivated">@color/colorPrimary</item> 
    <item name="android:textColorPrimary">@color/colorPrimary</item> 
</style> 

基本的にはエラーst editextの下に鳴り、editextを赤に設定しないでください。エラーを設定した後

私のUIは enter image description here

私の通常のUI、SETERRORにあなたが持っている enter image description here

+0

私は、設定されたエラーを使用したくないのEditTextスタイルから –

+0

https://stackoverflow.com/questions/33709066/how-to-set-textinputlayout-error-message-colour – Manifest

答えて

0

、である()ないTextInputLayoutになく、TextInputLayout内部のEditTextへ。

XML:ここでは、TextInputLayout内のEditTextのIDを割り当てました。

<android.support.design.widget.TextInputLayout 
    style="@style/textInputLayout" 
    android:id="@+id/fs_ti_email"> 

    <EditText 

     android:id="@+id/edit_text_email" <!-- here your id--> 
     style="@style/edittext" 
     android:tag="@string/str_email" 
     android:hint="@string/str_email"/> 
</android.support.design.widget.TextInputLayout> 

Javaは:ここで私はIDでのEditTextを見つけ、そこに()SETERRORを割り当てます。

EditText email; 

     //your code here... 
     ... 
    @Override 
    public void onClick(View v) { 
    switch (v.getId()) 
    { 
     case R.id.fs_bt_submit: 
     { 
      email.setError("Not a valid email address!"); 
     }break; 
    } 
    } 
+0

を背景を削除editextのメソッド。 – TheReprator

+0

どうしてですか?私には、EditTextにエラーを設定しても問題ありません。 – FARID

+0

EditTextの下のテキストの色のみを変更したい場合は、EditTextにsetError()を設定する必要があります。 – FARID

関連する問題