问题
Edit: I have looked at the error page for this; no answers work. It seems it is an Android
system bug that has not yet been solved.
First off I've referred to this similar question. But the solution to that question does not seem to be the solution to mine. I have a DialogFragment
that contains only a WebView
. Everything in the WebView
seems to be touchable. However, the problem is that when I touch a form field, the cursor appears but the soft keyboard never shows up!
Here's my code in the onCreateDialog()
method within the DialogFragment
class:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
WebView web = new WebView(getActivity());
web.loadUrl(InternetDialog.this.url);
web.setFocusable(true);
web.setFocusableInTouchMode(true);
web.requestFocus(View.FOCUS_DOWN);
web.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_UP:
if (!v.hasFocus()) {
v.requestFocus();
}
break;
}
return false;
}
});
builder.setView(web);
return builder.create();
How can I get the soft keyboard to show up when I select a form field?
回答1:
This is a system bug that has not yet been fixed. More information can be found here. It seems as though this bug occurs differently for people and therefore has different solutions. For my particular case, there is only one solution (as I've tried everything else). Solution:
First, I created a layout for the Dialog
:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:visibility="invisible"/>
<WebView
android:id="@+id/web"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
Then, in the DialogFragment
class in the onCreateDialog
method:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = LayoutInflater.from(getActivity());
View v = inflater.inflate(R.layout.internet_dialog, null);
WebView web = (WebView) v.findViewById(R.id.web);
EditText edit = (EditText) v.findViewById(R.id.edit);
edit.setFocusable(true);
edit.requestFocus();
web.loadUrl(url);
this.webView = web;
builder.setView(v);
return builder.create();
And that's all there was to it. The reason this worked was because I made an EditText
which I gave the focus to yet made invisible. Since the EditText
is invisible it doesn't interfere with the WebView
and since it has focus it pulls the soft keyboard up appropriately. I hope this helps any stuck in a similar situation.
来源:https://stackoverflow.com/questions/16550737/soft-keyboard-not-displaying-on-touch-in-webview-dialogfragment