好的,我的main.xml定义如下:

<LinearLayout android:id="@+id/linearLayout1" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">

    <EditText android:text="EditText" android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="175px"></EditText>
    <LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout2" android:layout_width="match_parent" android:layout_marginLeft="100px">
        <Button android:id="@+id/button1" android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="wrap_content" android:text="@string/btnPrint"></Button>
        <Button android:id="@+id/button2" android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="wrap_content" android:text="@string/btnCancel"></Button>
    </LinearLayout>
</LinearLayout>


我希望能够在调用setContentView(R.layout.main)之前从代码更新EditText的光标位置,这可能吗?如果不是,那么用于设置EditText视图的光标位置的xml属性是什么?

谢谢,

贾斯汀

最佳答案

在调用setContentView()之前,您将无法从Java端操作视图,因为除非实际在屏幕上显示视图,否则findViewById(R.id.yourView)将返回null。您是否有不想这样做的理由?

setContentView(R.layout.main);
EditText et = (EditText)findViewById(R.id.yourEditText);
et.setSelection(position);


尽管从技术上说,光标将在可见后移动,但它的速度太快了,用户不太可能注意到。

10-04 22:57
查看更多