Aju*_*Aju 146 android android-softkeyboard
我android:windowSoftInputMode="stateVisible"在Manifest中有一个Edittext .现在,当我开始活动时,将显示键盘.如何隐藏它?我无法使用,android:windowSoftInputMode="stateHidden因为当键盘可见时,最小化应用程序并恢复它键盘应该是可见的.我试过了
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
但它不起作用.
Nee*_*enu 348
在AndroidManifest.xml:
<activity android:name="com.your.package.ActivityName"
android:windowSoftInputMode="stateHidden" />
Run Code Online (Sandbox Code Playgroud)
或尝试
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)??;
Run Code Online (Sandbox Code Playgroud)
请检查这也
She*_*tib 202
使用以下功能显示/隐藏键盘:
/**
* Hides the soft keyboard
*/
public void hideSoftKeyboard() {
if(getCurrentFocus()!=null) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
}
/**
* Shows the soft keyboard
*/
public void showSoftKeyboard(View view) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
view.requestFocus();
inputMethodManager.showSoftInput(view, 0);
}
Run Code Online (Sandbox Code Playgroud)
man*_*ani 41
只需将两个属性添加到editText的父视图即可.
android:focusable="true"
android:focusableInTouchMode="true"
Run Code Online (Sandbox Code Playgroud)
San*_*esh 35
将它放在Activity标记内的清单中
android:windowSoftInputMode="stateHidden"
Run Code Online (Sandbox Code Playgroud)
Adn*_*nan 25
试试这个:
<activity
...
android:windowSoftInputMode="stateHidden|adjustResize"
...
>
Run Code Online (Sandbox Code Playgroud)
请看这一个了解更多细节.
小智 14
若要隐藏在新活动的时间softkeyboard启动或者onCreate(),onStart()等您可以使用下面的代码:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Run Code Online (Sandbox Code Playgroud)
使用AndroidManifest.xml
<activity android:name=".YourActivityName"
android:windowSoftInputMode="stateHidden"
/>
Run Code Online (Sandbox Code Playgroud)
使用Java
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
Run Code Online (Sandbox Code Playgroud)
使用上面的解决方案键盘隐藏但edittext在创建activiy时从焦点开始,但是当你用它们触摸它时抓住它:
添加您的EditText
<EditText
android:focusable="false" />
Run Code Online (Sandbox Code Playgroud)
还添加EditText的监听器
youredittext.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
v.setFocusable(true);
v.setFocusableInTouchMode(true);
return false;
}});
Run Code Online (Sandbox Code Playgroud)
将以下文本添加到xml文件中.
<!--Dummy layout that gain focus -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical" >
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我希望这会有效,我尝试了很多方法,但这个方法对我有用fragments.把这一行放在onCreateview/init上.
getActivity().getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Run Code Online (Sandbox Code Playgroud)
// In onResume, call this
myView.hideKeyboard()
fun View.hideKeyboard() {
val inputMethodManager = context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(windowToken, 0)
}
Run Code Online (Sandbox Code Playgroud)
基于用例的替代方案:
fun Fragment.hideKeyboard() {
view?.let { activity?.hideKeyboard(it) }
}
fun Activity.hideKeyboard() {
// Calls Context.hideKeyboard
hideKeyboard(currentFocus ?: View(this))
}
fun Context.hideKeyboard(view: View) {
view.hideKeyboard()
}
Run Code Online (Sandbox Code Playgroud)
fun Context.showKeyboard() { // Or View.showKeyboard()
val inputMethodManager = context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.toggleSoftInput(SHOW_FORCED, HIDE_IMPLICIT_ONLY)
}
Run Code Online (Sandbox Code Playgroud)
同时请求关注编辑文本时的更简单方法
myEdittext.focus()
fun View.focus() {
requestFocus()
showKeyboard()
}
Run Code Online (Sandbox Code Playgroud)
删除永远使用的要求getSystemService:Splitties 库
// Simplifies above solution to just
inputMethodManager.hideSoftInputFromWindow(windowToken, 0)
Run Code Online (Sandbox Code Playgroud)
将此代码放入您的java文件并在edittext上传递对象的参数,
private void setHideSoftKeyboard(EditText editText){
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}
Run Code Online (Sandbox Code Playgroud)
小智 5
要在“新活动”启动时或onCreate(),onStart()方法等时隐藏软键盘,请使用以下代码:
getActivity().getWindow().setSoftInputMode(WindowManager.
LayoutParams.SOFT_INPUT_STATE_HIDDEN);
Run Code Online (Sandbox Code Playgroud)
在活动中单击按钮时要隐藏软键盘:
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
assert imm != null;
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
Run Code Online (Sandbox Code Playgroud)
使用SOFT_INPUT_STATE_ALWAYS_HIDDEN而不是SOFT_INPUT_STATE_HIDDEN
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Run Code Online (Sandbox Code Playgroud)
以上答案也是正确的。我只想简要说明在启动 Activity 时有两种方法可以从 manifest.xml 中隐藏键盘。例如:
<activity
..........
android:windowSoftInputMode="stateHidden"
..........
/>
Run Code Online (Sandbox Code Playgroud)
或者
<activity
..........
android:windowSoftInputMode="stateUnchanged"
..........
/>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
196101 次 |
| 最近记录: |