软键盘没有出现
soft keyboard not appearing
我有以下对话框:
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);
}
}
我从另一个 activity 打开它,像这样:
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);
我还能尝试什么?这里有什么问题?
尝试在 menifext 文件中添加以下代码 -
<application
.....
android:hardwareAccelerated="true">
希望对您有所帮助。
试试这个
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);
}
});
您尝试过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);
}
});
}
试试这个 class
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) {
}
}
我有以下对话框:
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);
}
}
我从另一个 activity 打开它,像这样:
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);
我还能尝试什么?这里有什么问题?
尝试在 menifext 文件中添加以下代码 -
<application
.....
android:hardwareAccelerated="true">
希望对您有所帮助。
试试这个
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);
}
});
您尝试过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);
}
});
}
试试这个 class
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) {
}
}