0

我有一个EditText包裹在一个TextInputLayout. 在第一个输入文本后EditText,当焦点移动到另一个输入字段时,我想让之前的提示EditText消失。

我怎样才能实现这一目标?

4

1 回答 1

0

我猜你想要如果焦点从第一个输入字段变为第二个,那么第一个的提示应该消失。下面是做同样事情的代码。

活动主.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 回答