2009-10-02 135 views

回答

39

添加機器人:windowSoftInputMode =「stateAlwaysVisible」你的活動在AndroidManifest.xml文件:

<activity android:name=".MainActivity" 
android:label="@string/app_name" 
android:windowSoftInputMode="stateAlwaysVisible" /> 

在我的測試應用程序這顯示了應用程序的啓動鍵盤雖然是不固定的,但有可以通過按下後退按鈕來解除。

爲了確保鍵盤總是可見,您可能需要創建自己的鍵盤爲您的應用程序的用戶界面的一部分。下面是一個教程,向您展示如何使用KeyboardView完成此操作:http://www.fampennings.nl/maarten/android/09keyboard/index.htm

+0

我試圖使用Android源代碼,並可左右,但不知道哪一部分,我應該修改從隱藏禁用它拖動鍵盤。我正在看LatinIME.java下的這個類。我試圖將該代碼中的inputView添加到WindowManager.addView。 – LittleFunny 2016-10-04 13:14:58

+0

@Simon查看我對我的答案的更新。您可能想要使用KeyboardView。 – Intrications 2016-10-05 07:20:09

10

您的佈局中必須有EditText,並且需要擴展EditText基類。然後重寫onKeyPreIme()方法,並返回 True。現在您的鍵盤將始終可見,並且不能被後退鍵取消。

注意:由於您的onKeyPreIme()方法返回true您不能使用後退鍵退出您的應用程序。

例子:

public class CustomEdit extends EditText { 

    public CustomEdit(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     // TODO Auto-generated constructor stub 
    } 
    @Override 
    public boolean onKeyPreIme(int keyCode, KeyEvent event) { 
     // TODO Auto-generated method stub 
     Log.e("Log", "onKeyPreIme"); 
     return true; 
     //return super.onKeyPreIme(keyCode, event); 
    } 
} 

onKeyPreIme() - Android developer

0

我發現,工作對我來說,保持軟鍵盤的編輯後,可見我EditText類的myEditText領域的方式。關鍵是要覆蓋onEditorAction方法,使其返回true

myEditText.setOnEditorActionListener(new OnEditorActionListener() {      
    @Override 
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
     return true; 
    }  
    }); 

否則只有「完成」鍵,點擊後有onEditorAction返回trueIME_ACTION_DONE),否則false

myEditText.setOnEditorActionListener(new OnEditorActionListener() {      
    @Override 
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
     if(actionId==EditorInfo.IME_ACTION_DONE){ 
     Log.i(LOG_TAG, "IME_ACTION_DONE"); 
     return true;  
     } 
     return false; 
    }  
    }); 

(見this answeronEditorAction方法)

android:windowSoftInputMode="stateAlwaysVisible添加到清單文件幫助在活動開始時顯示軟鍵盤,但是在編輯後單擊「完成」鍵時不會阻止它再次消失。