2011-12-19 42 views
2

設置我有一個Android應用程序,在它的一個組成部分的鍵盤工作正常,除了那 - 雖然用下面的代碼打開 - 沒有出現:Android不顯示鍵盤雖然在代碼

View.OnFocusChangeListener vof = new View.OnFocusChangeListener() { 
    public void onFocusChange(View v, boolean hasFocus) { 
     if (hasFocus) { 
      getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); 
     } 
    } 
}; 

// ... 

final TextView input = (TextView) findViewById(R.id.answer); 
input.setOnFocusChangeListener(vof); 
input.requestFocus(); 

我在其他幫助頁面上可以找到的所有東西都只有這個解決方案,當我嘗試在我的Samsung Galaxy S2上使用它時,這種解決方案無效。

+0

你確定onFocusChange被調用嗎? – njzk2 2011-12-19 17:49:24

+0

@ njzk2我確定它是 - android的sdk/adt for eclipse自動添加了這個方法,我不認爲這是錯誤的。此外,此代碼適用於此應用中的另一個textedit。 – hlt 2011-12-19 18:35:12

+0

我現在找到了解決方案...查看我的編輯 – hlt 2011-12-19 19:19:04

回答

0

我發現這個問題 - 在XML佈局要求的焦點之前,我設置的偵聽器,以便onFocusChange從來沒有所謂:

<EditText 
    android:id="@+id/answer" 
    android:layout_width="wrap_content" 
    android:layout_height="175dp" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 
    android:layout_below="@+id/question" 
    android:gravity="top" 
    android:inputType="textMultiLine"> 

    <requestFocus /> 
</EditText> 

當我刪除了它的工作,因爲我想它< requestFocus的/ >部分至。

+1

請注意,編輯原始問題的答案(即使您問了問題)是在StackOverflow上提出答案的正確方法。如果你找到自己問題的答案,你應該將其作爲答案發布,以便其他人可以對其進行投票。如果您覺得答案可以解決您的問題,那麼您可以通過選中答案下方左側的綠色複選框來接受答案。 – casperOne 2011-12-20 13:08:05