单击或触摸另一个视图时清除EditText焦点并隐藏键盘

问题描述 投票:0回答:2

我试图隐藏键盘和清除光标(也无法输入内容)。

我正在使用edittext,FrameLayout(它只是容器)和textView(具有clickListener)制作应用程序。当我在edittext中输入内容然后触摸或单击其他视图时,我想隐藏键盘(显示)并清晰地关注edittext(显示光标)。

这是我的代码。

main A磁体vi同意.Java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    RelativeLayout mainLayout;

    EditText editText;
    FrameLayout container;
    TextView textView;

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

        editText = (EditText)findViewById(R.id.editText);
        editText.setFocusableInTouchMode(true);
        editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                Log.d("onFocusChange", "b #" + b);
                // i can only get true, i can't get false value. how can i get false value.
                // i want to use this method so hard!
            }
        });


        textView = (TextView)findViewById(R.id.textView);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // hide Keyboard and editText's focus will be false(also cursor is going to hide).
            }
        });

        container = (FrameLayout)findViewById(R.id.container);
        container.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                // hide Keyboard and editText's focus will be false(also cursor is going to hide).
                return true;
            }
        });
    }
}

activity_main.xml(这只是具有EditText,frameLayout和TextView的简单布局)

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"

    android:focusable="true"
    android:focusableInTouchMode="true" >

    <EditText
        android:id="@+id/editText"

        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <FrameLayout
        android:id="@+id/container"

        android:background="@android:color/holo_blue_bright"

        android:layout_above="@+id/textView"
        android:layout_below="@+id/editText"

        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </FrameLayout>

    <TextView
        android:id="@+id/textView"

        android:gravity="center"

        android:layout_alignParentBottom="true"
        android:text="textView : layout_alignParentBottom = true"

        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</RelativeLayout>

最后一件事,AndroidManifest.xml

我只是插入代码:<activity ... android:windowSoftInputMode="adjustResize"> ... </activity>

android android-edittext
2个回答
1
投票

final添加editText = (EditText)findViewById(R.id.editText);

试试这个

textView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);

        }
});

要么

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean b) {
            if (!b) {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
            }

        }
});

我希望这对你有所帮助


1
投票

你必须得到editText的onFocusChange

代码可以是这样的。

 editText.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (v.getId() == R.id.editText && !hasFocus) {
                    InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);

                }
            }
        });

即使您使用片段滑动,此代码也会起作用。(对于片段,将mContext更改为getActivity());

© www.soinside.com 2019 - 2024. All rights reserved.