调用onBackPressed

时间:2015-10-23 07:08:07

标签: android android-edittext

当我用EditText在键盘上按back_键时,我想在EditText中清除焦点(这意味着无法输入内容。并隐藏光标)

这是问题所在。当我按后退键一次。刚刚消失的键盘。 我再次按,现在我可以从onBackPressed方法获得日志。

我想清除这些(键盘和光标[意思是不能输入内容])

请给我一些提示。

这是我的代码,

MainActivity.java

public class MainActivity extends AppCompatActivity {

    RelativeLayout mainLayout;

    EditText editText;
    FrameLayout container;
    TextView textView;


    @Override
    public void onBackPressed() {

        if(editText.isFocused()){
            Log.d("onBackPressed", "foo bar!");
            // here is problem. doesn't work when press back-key first-time,
            // meaning is can't get log. only keyboard just disappear.
            // but i want to clear these(keyboard and cursor) at once.
        }

//        super.onBackPressed();
    }

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

        textView = (TextView)findViewById(R.id.textView);
        textView.setFocusableInTouchMode(true); textView.requestFocus();
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("onClick", "textView");
            }
        });
        container = (FrameLayout)findViewById(R.id.container);
        container.setFocusableInTouchMode(true); container.requestFocus();
        container.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("onClick", "container");
            }
        });

        editText = (EditText)findViewById(R.id.editText);
        editText.setFocusableInTouchMode(true); editText.requestFocus();
        editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                Log.d("onFocusChange", "hasFocus #" + hasFocus);
                if (view.getId() == R.id.editText && !hasFocus) { // if (!hasFocus) {
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
                }
            }
        });

    }
}

和简单的布局。拥有EditText,FrameLayout(只是容器)和TextView(shwoing parent' s height更改为android:windowSoftInputMode="adjustResize"

<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" >

    <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>

2 个答案:

答案 0 :(得分:0)

试试这个, 在你的布局中添加这些行

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

同样要隐藏键盘,请在onBackPressed()中添加:

((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(myEditText.getWindowToken(), 0);

答案 1 :(得分:0)

textview.setFocusableInTouchMode(false);
textview.clearfocus();

添加此