2014-03-06 87 views
1

我有一個EditText和一個CountDownTimer。通常情況下,如果你用EditText開始一個活動,EditText被激活並且鍵盤顯示出來。但我希望活動啓動,計時器關閉,然後鍵盤自動顯示。 我該怎麼做?如何在延遲時間後激活(打開鍵盤)EditText?

new CountDownTimer(5000, 1000) { 

     public void onTick(long millisUntilFinished) { 
      //Do Something 
     } 

     public void onFinish() { 
      //Keyboard pop up 
     } 
     }.start(); 
+0

讓我錯了,如果我錯了,你想,當活動開始,你不想要專注編輯文本和你的後計時器下降,然後你想顯示鍵盤。所以你可以做的是,當活動負載使你編輯文本可聚焦的假,並在計時器任務後使其可聚焦真正和請求焦點到你的編輯文本。 –

回答

6

我建議使用HandlerpostDelayed(...)方法及您指定的延遲時間後,打開鍵盤:

@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> 
+0

兩者都不適合我。什麼都沒有出現。或者它是最終的EditText? – L3n95

+0

它需要是final或成員變量,否則在run()方法內不可見。 –

+0

還沒有顯示出來。我第一次(在onCreate)必須刪除焦點或禁用EditText? – L3n95