我建議使用Handler
與postDelayed(...)
方法及您指定的延遲時間後,打開鍵盤:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText et= (EditText) findViewById(R.id.editText1);
Handler h = new Handler();
h.postDelayed(new Runnable() {
@Override
public void run() {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(et, InputMethodManager.SHOW_IMPLICIT);
}
}, 5000); // delay in milliseconds
}
您所需的延遲時間後,這將打開鍵盤。
這裏我.xml文件:(請注意<requestFocos />
標籤)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="30dp"
android:ems="10" >
<requestFocus />
</EditText>
</RelativeLayout>
讓我錯了,如果我錯了,你想,當活動開始,你不想要專注編輯文本和你的後計時器下降,然後你想顯示鍵盤。所以你可以做的是,當活動負載使你編輯文本可聚焦的假,並在計時器任務後使其可聚焦真正和請求焦點到你的編輯文本。 –