如何在Android中选择相同的布局中一个EditText专注于网页视图?视图、布局、网页、专注于

2023-09-06 15:47:35 作者:蒾夨°

我一直在尝试添加EditTexts并在网页视图布局。麻烦的是侧重于的WebView。我曾尝试通过网络和对#1的答案决定再次询问的人之前的样子。

在web视图,我有一些字段填写,这是通过POST发送。如果我选择的第一个网页视图(然后在该字段中的值),它的罚款。但是,如果我先选择EditTexts,我无法专注于web视图填写网页上的字段,但似乎又光标设置平行于一个在的EditText部分。由此产生的网页包含的一些信息,我将需要添加到EditTexts。

我的code迄今:

 公共类LoadWebandEditText延伸活动{@覆盖公共无效的onCreate(捆绑savedInstanceState){    super.onCreate(savedInstanceState);    的setContentView(R.layout.oauth_signin);    最终的WebView网页=(的WebView)findViewById(R.id.webview_page);    webpage.setWebViewClient(新WebViewClient());    webpage.loadUrl(www.towardsmypage.com);    按钮btn_next =(按钮)findViewById(R.id.btn_next);    最终的EditText edittext_1 =(EditText上)findViewById(R.id.editText_1);    最终的EditText edittext_2 =(EditText上)findViewById(R.id.EditText_2);    webpage.setOnClickListener(新View.OnClickListener(){          公共无效的onClick(视图v){              webpage.requestFocus();              webpage.setFocusable(真);          }        });    webpage.setOnFocusChangeListener(新OnFocusChangeListener(){        公共无效onFocusChange(查看arg0中,布尔hasFocus){            如果(!hasFocus){                webpage.requestFocus();                webpage.setFocusable(真);            }        } }); 

我Layout.xml:

 <?XML版本=1.0编码=UTF-8&GT?;< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android    机器人:layout_width =match_parent    机器人:layout_height =match_parent    机器人:方向=垂直>    <的WebView        的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android        机器人:ID =@ + ID / webview_page        机器人:layout_width =FILL_PARENT        机器人:layout_height =WRAP_CONTENT        机器人:layout_weight =0.7        机器人:可聚焦=真/>    <的TextView            机器人:ID =@ + ID / TextView01            机器人:layout_width =WRAP_CONTENT            机器人:layout_height =WRAP_CONTENT            机器人:paddingLeft =15dip            机器人:文字=输入字段信息1            机器人:TEXTSIZE =18dp            机器人:文字样式=大胆            机器人:字体=SANS/>    <的EditText        机器人:ID =@ + ID / editText_1        机器人:layout_width =match_parent        机器人:layout_height =WRAP_CONTENT        机器人:EMS =10        安卓的inputType =textPassword>    < /&的EditText GT;    <的TextView        机器人:ID =@ + ID / textView1        机器人:layout_width =WRAP_CONTENT        机器人:layout_height =WRAP_CONTENT        机器人:paddingLeft =15dip        机器人:文字=输入字段信息2        机器人:TEXTSIZE =18dp        机器人:文字样式=大胆        机器人:字体=SANS/>    <的EditText        机器人:ID =@ + ID / EditText_2        机器人:layout_width =match_parent        机器人:layout_height =WRAP_CONTENT        机器人:EMS =10        安卓的inputType =textPassword/>    <按钮        机器人:ID =@ + ID / btn_next        风格=机器人:ATTR / buttonStyleSmall        机器人:layout_width =WRAP_CONTENT        机器人:layout_height =WRAP_CONTENT        机器人:文字=按钮        机器人:layout_gravity =CENTER_HORIZONTAL/>< / LinearLayout中> 
国产操作系统首次确认, 与ios系统一样完美, 可以取代安卓系统

解决方案

我在几个星期前遇到同样的问题。答案很简单,一个onTouchListener设置为网页视图,然后将其设置如下:

 公共布尔onTouch(视图V,MotionEvent事件){    开关(event.getAction()){    案例MotionEvent.ACTION_DOWN:    案例MotionEvent.ACTION_UP:        如果(!v.hasFocus()){            v.requestFocus();        }        打破;    }    返回false;} 

编辑:逸岸,我发现在相关类别完全相同的副本,请参阅这个。

让我知道,如果你要寻找的。不是

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>

解决方案

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.