android ImageView onTouch打开软输入键盘并请求专注于EditText(类似于环聊)

时间:2014-09-06 06:53:47

标签: android view android-softkeyboard

open keyboard action

我想在我的应用 imageview onClick 打开键盘上做类似的功能,我已经完成了键盘打开非常好。

我使用相对布局我已将键盘图像放在 edittext 上,这类似于此参考环聊图像。

问题是当我输入某些内容时,它不会显示在edittext的文本字段中。怎么处理?指导我

    protected void onCreate(Bundle savedInstanceState) {

    imageview = (ImageView) findViewById(R.id.keyboard);
    imageview.setFocusableInTouchMode(true);
    imageview.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                try {
                        showSoftKeyboard(imageview);
                } catch (Exception e) {
                    Log.e("Error MA onCreate keyboard open", "Error");
                }
                break;
            }

            return true;
        }
    });

}

public void showSoftKeyboard(View view) {
    if (view.requestFocus()) {
        InputMethodManager imm = (InputMethodManager)
                getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
    }
}

这是我的xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
    android:id="@+id/search_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:padding="12dp" >

    <ImageView
        android:id="@+id/keyboard"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:focusable="true"
        android:src="@drawable/ic_action_keyboard" />

    <EditText
        android:id="@+id/search"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/keyboard"
        android:hint="Search Here"
        android:focusable="true" />
</RelativeLayout>
<ListView
/>
</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

问题:imageView正在调用软键盘。

可能的解决方案:使用edittext作为参数点击imageView调用showSoftKeyboard()方法。

public void showSoftKeyboard(View view) {
if (view.requestFocus()) {
   // myEditText.requestFocus();//this line is also worth a try
    InputMethodManager imm = (InputMethodManager)
            getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}
}

答案 1 :(得分:0)

    public void showSoftKeyboard(View view) {
    if (view.requestFocus()) {
        mEditText.setFocusable(true);
        mEditText.requestFocus();// this line is also worth a try
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(mEditText, InputMethodManager.SHOW_IMPLICIT);//sending mEditText here in the parameter solved my problem instead of sending view

    }
}