弹出窗口中的EditText即使设置了setFocusable(true),也不显示键盘

19

我似乎无法使这个工作起来。我已经按照我在其他论坛上读到的设置了popWindow可聚焦,但仍然没有成功。

xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:background="@drawable/popbg"
android:orientation="vertical" >

<Button
    android:id="@+id/cancel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:layout_marginRight="10dp"
    android:layout_marginTop="30dp"
    android:background="@drawable/zcancel" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:layout_marginLeft="10dp"
    android:text="SSID"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
    android:id="@+id/editText3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="textPersonName" />

Java

 case(R.id.settings):
 switch (event.getAction()) 
 {
case MotionEvent.ACTION_DOWN:
v.setBackgroundResource(R.drawable.cpanel2);
return true;
case MotionEvent.ACTION_UP:
v.setBackgroundResource(R.drawable.cpanel1);

LayoutInflater layoutInflater =  
    (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);  

View popSwitchView = layoutInflater.inflate(R.layout.settings_xml, null);

final PopupWindow popWindow = new PopupWindow(popSwitchView);
popWindow.setWidth(LayoutParams.MATCH_PARENT);
popWindow.setHeight(LayoutParams.MATCH_PARENT);
popWindow.showAtLocation(popSwitchView, Gravity.CENTER, 0, 0);
popWindow.setOutsideTouchable(false);  
popWindow.setFocusable(true);
Drawable d = getResources().getDrawable(R.drawable.popbg); 
popWindow.setBackgroundDrawable(d);

Button CancelButton = (Button)popSwitchView.findViewById(R.id.cancel);

CancelButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
popWindow.dismiss();
}
});

                    popWindow.showAsDropDown(v, 50, -30);

                    return true;
                default:
                    return false;


            }

我计划创建一个用于网络配置的弹出设置窗口。但我似乎无法修复我的代码,以便为大家提供良好的视图。


尝试使用这个和这个链接:http://v4all123.blogspot.in/2013/09/custom-dialogfragmnet-example-in-android.html 和 http://v4all123.blogspot.in/2013/06/return-values-from-custom-popup-window.html。 - Gunaseelan
能不能用弹出窗口实现呢?我需要使用对话框片段吗?而且我已经把所有东西都放在第二个里了,我真的不明白为什么还是没有反应,不起作用。 - yhunz_19
5个回答

60

正如爱尔兰人所说,“上帝的名字是什么意思?” - Daniel Wilson
popupWindow.setFocusable(true); popupWindow.update(); 对我有效。 - Denver Shenal

5
这是我的解决方案:
该方法对我有效:
popWindow.setFocusable(true);
popWindow.update();

David,感谢您的反馈,但它对我来说是有效的并且运行良好。为什么您投了-1呢?我不明白。 - Kamleshwer Purohit

1
在显示弹出窗口之前,请尝试添加以下行:

popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

1

没有任何帮助,仍然无法在某些设备上显示,所以我不得不像这样强制显示它(kotlin):

val editText = customView.findViewById<EditText>(numberFieldId)
        editText.onClick {
            showKeyboard(editText, context)
        }

private fun showKeyboard(mEtSearch: EditText, context: Context) {
        mEtSearch.requestFocus()
        val imm = context.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0)
    }

-1
试试这个:
编程方式:
edittext.requestFocus();

通过 XML:

<EditText
android:id="@+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName">
    <requestFocus />
</EditText>

希望能有所帮助!


这应该是所选答案 - blackHawk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接