Tôi gặp vấn đề khó chịu với màn hình. Màn hình bao gồm một loạt các Spinners một dưới khác, và sau đó bên dưới spinner, một EditText.Không thể quản lý để requestFocus một Spinner
Vấn đề là khi màn hình bắt đầu, EditText đã tập trung, có nghĩa là một số Spinners đang ở phía trên cùng của màn hình. Hãy thử như tôi có thể, tôi không thể làm cho Spinner hàng đầu bắt đầu tập trung, hoặc bằng cách sử dụng <requestFocus/>
trong màn hình XML, hoặc bằng cách sử dụng requestFocus()
trong mã. Tôi đã cố gắng để làm những gì requestFocus skipping next EditText gợi ý, và nếu tôi đang làm theo đề xuất một cách chính xác, nó cũng không hoạt động.
Để tái tạo sự cố, hãy tạo một dự án Android mới trong Eclipse. main.xml là
<?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">
<Spinner
android:id="@+id/spinner"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<requestFocus />
</Spinner>
<EditText
android:id="@+id/edittext"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Mã hoạt động là
package nz.co.kb.testspinner;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
public class TestSpinner extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
View view = getLayoutInflater().inflate(R.layout.main, null);
final View spinner = view.findViewById(R.id.spinner);
view.post(new Runnable() {
public void run() {
spinner.requestFocus();
}
});
setContentView(view);
spinner.requestFocus();
}
}
Lưu ý nhiều phong cách của requestFocus cố gắng.
Đây có phải là lỗi nền tảng hay tôi đang làm điều gì đó sai?
Cảm ơn.
Đây là câu trả lời đúng. Mặc dù tôi không thể làm cho nó hoạt động trong xml. android: focusable = "true" android: focusableInTouchMode = "true" – MinceMan
Nó hoạt động sau khi thêm 's1..requestFocus();' –