请求焦点跳过下一个EditText。

8
我在控制焦点方面遇到了一些问题。我的界面定义如下:
源(RadioGroup / 可选) 目的地(EditText) 数量(EditText) 传输(按钮)
我在代码中更改“源”的可见性。当我不显示它时,焦点自动转移到“数量”,而我希望它转移到“目的地”上。我不知道我错过了什么。我怀疑这可能是Android的一个bug,但我不确定。有人知道如何解决吗?
谢谢。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <RadioGroup android:id="@+id/source"
     android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:orientation="vertical"
   android:visibility="gone">
   <RadioButton android:id="@+id/default"
    android:checked="false"
    android:text="@string/default"/>
 </RadioGroup>

 <TableLayout
  android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:stretchColumns="1">
     <TableRow>
   <TextView 
    android:text="@string/destination"
    android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:textSize="16sp"
       android:textColor="#ffffff"
       android:paddingRight="10sp"
       android:layout_weight="1"/>

      <EditText android:id="@+id/destination"
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content"
       android:layout_weight="1"
       android:singleLine="true"><requestFocus/></EditText>
  </TableRow>

  <TableRow>
   <TextView 
    android:text="@string/quantity"
    android:layout_width="wrap_content" 
       android:layout_height="wrap_content"
       android:textSize="16sp"
       android:textColor="#ffffff"
       android:paddingRight="10sp"
       android:layout_weight="1"/>

      <EditText android:id="@+id/quantity"
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content"
       android:layout_weight="1"
       android:singleLine="true"/>
  </TableRow>
 </TableLayout>

 <Button android:id="@+id/transfer"
  android:layout_width="fill_parent" 
     android:layout_height="wrap_content"
     android:text="@string/transfer"/>
</LinearLayout>

我正在Android 2.2上进行测试,但无法重现问题,重点在于一开始的目标地。如果您继续遇到问题,我建议在您的onResume方法中添加EditText e = (EditText) findViewById(R.id.destination); e.requestFocus(); - Sebastian Roth
感谢您的回复。是的,但我正在Android 2.1上进行测试。requestFocus()无法解决问题。 - pocoa
请查看我在以下主题中的回答:https://dev59.com/YGgu5IYBdhLWcg3wgXO5#13194129 - iBog
4个回答

8

谢谢提供链接。是的,我尝试使用destination.requestFocus(),但它没有起作用。 - pocoa
7
谢谢。使用以下代码,问题得到了解决。 this.post(new Runnable() { public void run() { text.requestFocus(); } }); - pocoa

3

我遇到了相同的问题, 尝试一下这个代码,它对我有用。

             editText.post(new Runnable() {
                 public void run() {
                       editText.requestFocus(); 
                                   } 
                                           }); 

当使用焦点方法时,我的视图没有被突出显示。我也尝试过这个,但是没有用。 - AndroidOptimist

2

来自在线文档

如果一个视图不可聚焦(isFocusable()返回false),或者如果它是可聚焦的并且在触摸模式下不可聚焦(isFocusableInTouchMode()),则视图实际上不会获取焦点,当设备处于触摸模式时。


1
使用EditTextrequestFocus()方法。 :)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接