未显示Android软键盘

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

我有以下对话框:

import android.app.AlertDialog;

public class IpDiscoveryDialog extends AlertDialog implements View.OnClickListener {

    private View view;
    private Context context;
    private Activity activity;

    private EditText ipEditText;


    protected IpDiscoveryDialog(Context context) {
        super(context);
        this.context = context;
    }

    public IpDiscoveryDialog(Context context, Activity activity) {
        this(context);
        this.activity = activity;
    }

    @Override
    public void setContentView(int layoutResID) {
        View view = getLayoutInflater().inflate(layoutResID, null);
        this.view = view;
        this.setContentView(view);
    }

    @Override
    public void setContentView(@NonNull View view) {
        super.setContentView(view);
        this.buildView(view);
        this.setCancelable(false);
    }

    private void buildView(View view) {
        view.findViewById(R.id.ipConfirmButton).setOnClickListener(this);
        this.ipEditText = view.findViewById(R.id.ipEditText);
    }
}

我从类似的活动中打开它:

IpDiscoveryDialog ipDiscoveryDialog = new IpDiscoveryDialog(ScanDevicesActivity.this, ScanDevicesActivity.this);
ipDiscoveryDialog.create();
ipDiscoveryDialog.setContentView(R.layout.ip_discovery_dialog);
ipDiscoveryDialog.show();

并且对话框的布局如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp">

    <TextView
        android:id="@+id/textView8"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/set_ip_address"
        android:textAlignment="center"
        android:textColor="@android:color/black"
        android:textSize="24sp" />

    <EditText
        android:id="@+id/ipEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/ip_address"
        android:importantForAutofill="no"
        android:inputType="text" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/ipConfirmButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/confirm" />
</LinearLayout>

我的问题是,无论做什么,我都无法打开软键盘来编辑该EditText。我尝试用代码手动打开它,尝试了不同的设置,但没有任何效果。

这样的代码不起作用:

InputMethodManager mImm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);  
mImm.showSoftInput(SearchEdit, InputMethodManager.SHOW_IMPLICIT);

我还能尝试什么?这是什么问题?

java android android-alertdialog android-softkeyboard
4个回答
0
投票

尝试在menifext文件中添加以下代码-

 <application
    .....
    android:hardwareAccelerated="true">

希望这会对您有所帮助。


0
投票

尝试一下

    ipDiscoveryDialog .setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {

            ((InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
        }
    });

0
投票

您是否尝试过post showSoftInput?我为此使用了实用程序功能:

public static void showSoftKeyboard(EditText v) {
    Context c = v.getContext();
    InputMethodManager keyboard = (InputMethodManager)
            c.getSystemService(Context.INPUT_METHOD_SERVICE);
    v.requestFocus();
    keyboard.showSoftInput(v, 0);
}

public static boolean postShowSoftKeyboard(final EditText v) {
    return v.post(new Runnable() {
        @Override
        public void run() {
            showSoftKeyboard(v);
        }
    });
}

0
投票

尝试这个课程

public class IpDiscoveryDialog extends AlertDialog implements View.OnClickListener {

private View view;
private Context context;
private Activity activity;

private EditText ipEditText;


protected IpDiscoveryDialog(Context context) {
    super(context);
    this.context = context;
}

public IpDiscoveryDialog(Context context, Activity activity) {
    this(context);
    this.activity = activity;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.ip_discovery_dialog);
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    buildView();
    this.setCancelable(false);
}

private void buildView() {
    findViewById(R.id.ipConfirmButton).setOnClickListener(this);
    this.ipEditText = findViewById(R.id.ipEditText);
}

@Override
public void onClick(View v) {

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