0

内にEditTextラップされた がありTextInputLayoutます。最初にテキストを入力した後、フォーカスが別の入力フィールドに移動したときに、前のヒントが消えるEditTextようにしたいと考えています。EditText

これを達成するにはどうすればよいですか?

4

1 に答える 1

0

フォーカスが最初の入力フィールドから2番目の入力フィールドに変更された場合、最初のヒントが消えるはずです。以下は、同じことを行うコードです。

activity_main.xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true">

<android.support.design.widget.TextInputLayout
    android:id="@+id/first_text_input_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="hello">

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

<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/first_text_input_layout">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="hello" />
</android.support.design.widget.TextInputLayout>
</RelativeLayout>

MainActivity.java :

package com.example.gaurav.myapplication;

import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;


public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final TextInputLayout textInputLayout = (TextInputLayout) findViewById(R.id.first_text_input_layout);
    EditText editText = (EditText) findViewById(R.id.first_edit_text);

    editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
             //Below code will check if editext has focus then show hint 

             //else if it dosen't have focus and user has entered some value only then hide hint.
            if (hasFocus){
                textInputLayout.setHint("hello");
            } else if ( !TextUtils.isEmpty(editText.getText().toString().trim())){
                textInputLayout.setHint("");

            }
        }
    });
}
}
于 2016-05-29T11:47:32.690 に答える