开发者

Android: Can you edit a view created in xml, in code?

Ok I have 开发者_如何学运维my main.xml defined like:

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

I want to be able to update the EditText's cursor position from code before the call to setContentView(R.layout.main), is this possible? If not, what is the xml attribute for setting the cursor position of an EditText View?

Thanks,

Justin


You can not do any modifications to the views, which are not yet inflated. So, the first answer is No.

As for the second try this tag <requestFocus /> within your edit text:

<EditText ...>
  <requestFocus />
<EditText/>


You won't be able to manipulate your view from the java side before your setContentView() call, because findViewById(R.id.yourView) will return null unless the view in question is actually on the screen. Is there a reason you don't want to do it like this?

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

Although the cursor will technically move after its visible, it is going to be so fast that users are unlikely to notice.


Inflate the view, then set the cursor position or make any changes that you want, then set pass this view to setContentView() method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜