How to focus on Webview upon selecting an EditText

2020-03-30 00:37发布

问题:

I have been trying to add EditTexts and WebView in a layout. The trouble is with focussing on WebView. I have tried to look through the answers on the web and Stackoverflow before deciding to ask one again.

In the webview, I have some fields to fill in, which are sent via POST. If I select the Webview first (then enter the values in the field), it's fine. However, if I select the EditTexts first, I cannot focus on the Webview to fill in the fields on the webpage, although it seems that another cursor is setup in parallel to the one in the EditText section. The resulting webpage contains some information which I will require to add to the EditTexts.

My code so far:

public class LoadWebandEditText extends Activity {

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    setContentView(R.layout.oauth_signin);
    final WebView webpage = (WebView)findViewById(R.id.webview_page);
    webpage.setWebViewClient(new WebViewClient());
    webpage.loadUrl("www.towardsmypage.com");


    Button btn_next = (Button)findViewById(R.id.btn_next);
    final EditText edittext_1 = (EditText)findViewById(R.id.editText_1); 
    final EditText edittext_2 = (EditText)findViewById(R.id.EditText_2);


    webpage.setOnClickListener(new View.OnClickListener() {
          public void onClick(View v) {
              webpage.requestFocus();
              webpage.setFocusable(true);
          }
        });

    webpage.setOnFocusChangeListener(new OnFocusChangeListener(){

        public void onFocusChange(View arg0, boolean hasFocus) {


            if (!hasFocus){
                webpage.requestFocus();
                webpage.setFocusable(true);
            }
        }
 });

My Layout.xml:

<?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" >

    <WebView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/webview_page"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.7" 
        android:focusable="true"/>

    <TextView
            android:id="@+id/TextView01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="15dip"
            android:text="Enter Field Information 1"
            android:textSize="18dp"
            android:textStyle="bold"
            android:typeface="sans" />

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


    </EditText>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="15dip"
        android:text="Enter Field Information 2"
        android:textSize="18dp"
        android:textStyle="bold"
        android:typeface="sans" />

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

    <Button
        android:id="@+id/btn_next"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" 
        android:layout_gravity="center_horizontal" />
</LinearLayout>

回答1:

I encountered the same problem a few weeks ago. The answer is simple, set an onTouchListener to the webview and then set it up as follows:

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;
}

Edit: Infact I found an exact duplicate in the "Related" category, read this.

Let me know if its not what you're looking for.