我的布局中有一个EditText和一个Button.
在编辑字段中写入并单击后Button,我想隐藏虚拟键盘.我假设这是一段简单的代码,但我在哪里可以找到它的一个例子?
android soft-keyboard android-layout android-softkeyboard android-input-method
我是Android的新手.我已经花了两个小时搜索.无论我尝试使用软键盘都不会显示给我EditText.我简单地创建它:
EditText editText = (EditText)findViewById(R.id.editText);
Run Code Online (Sandbox Code Playgroud)
我试过了:
editText.requestFocus();//i tried without this line too
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
Run Code Online (Sandbox Code Playgroud)
和:
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus)
{
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
});
Run Code Online (Sandbox Code Playgroud)
我也尝试过:
getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
Run Code Online (Sandbox Code Playgroud)
我试着将这一行放入AndroidManifest.xml文件中:
android:windowSoftInputMode="stateVisible|adjustResize"
Run Code Online (Sandbox Code Playgroud)
但一切都是徒劳的.它永远不会显示.我错过了什么?
在我看来,我有一个搜索EditText,我想以编程方式触发字段上的单击事件的行为,即将焦点放在文本字段上并在必要时显示软键盘(如果没有可用的硬键盘).
我试过了field.requestFocus().该字段实际上获得焦点但不显示软键盘.
我试过了field.performClick().但那只调用该字段的OnClickListener.
任何的想法 ?
请解释一下软键盘的问题.例如,我在我的活动或对话片段或片段活动上有一个EditText,无论如何.这里是:
<EditText
android:id="@+id/edPswrd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword" >
<requestFocus />
</EditText>
Run Code Online (Sandbox Code Playgroud)
当它第一次显示我没有看到软键盘并且必须按editText才能获得焦点并且键盘出现.另一个活动是不同的,当它出现在屏幕上时,键盘在没有任何帮助的情况下加载.我以为
<requestFocus />
意味着EditText将被聚焦并且键盘将出现,但我错了.
如何管理哪些组件将获得焦点,键盘将自动显示.
我有一个Fragment(兼容版本)的EditText布局.我正在使用a ViewFlipper在片段之间翻转.当我遇到这个问题时Fragment,软键盘会自动打开.这不是我想要的.这是我试图阻止它或隐藏它.
尝试:
android:descendantFocusability="beforeDescendants"
Run Code Online (Sandbox Code Playgroud)
在片段的主视图上
尝试:
android:windowSoftInputMode="stateHidden"
Run Code Online (Sandbox Code Playgroud)
和
android:windowSoftInputMode="stateAlwaysHidden"
Run Code Online (Sandbox Code Playgroud)
在活动的清单中
尝试:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mViewPager.getChildAt(position).getWindowToken(), 0);
Run Code Online (Sandbox Code Playgroud)
在我的ViewPager的OnPageChangeListener上
尝试:
InputMethodManager imm = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(voucherView.findViewById(R.id.redeem_mobile_number).getWindowToken(), 0);
Run Code Online (Sandbox Code Playgroud)
在我的片段中的onCreateView中
尝试:
InputMethodManager imm = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getView().findViewById(R.id.redeem_mobile_number).getWindowToken(), 0);
Run Code Online (Sandbox Code Playgroud)
在我的片段中的onStart中
尝试:
voucherView.findViewById(R.id.redeem_mobile_number).clearFocus();
Run Code Online (Sandbox Code Playgroud)
在我的片段中的onCreateView中
在我看来,onPageChangeListener就是这样做的地方,因为其他调用在软键盘实际打开之前发生.任何帮助都会很棒.
我正在尝试关闭在另一个应用程序中打开的软键盘.我从这里尝试了所有解决方案:以 编程方式隐藏/显示Android软键盘或此处:关闭/隐藏Android软键盘
正如你在图片中看到我必须关闭从另一个应用程序打开的键盘,添加到清单,不要让键盘可见不起作用.
要注意这是一个更衣室应用程序,我会在手机进入睡眠模式时启动一项活动.
我错过了什么吗?从商店测试其他更衣室应用程序并没有遇到此问题
但结果如下:

编辑:更多信息
这是我启动储物柜的方式:
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
//Toast.makeText(context, "" + "screeen off", Toast.LENGTH_SHORT).show();
wasScreenOn = false;
Intent intent = new Intent(context, LockScreenActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivity(intent);
// do whatever you need to do here
//wasScreenOn = false;
}
Run Code Online (Sandbox Code Playgroud)
这是清单代码:
<activity
android:name=".ui.activities.LockScreenActivity"
android:excludeFromRecents="true"
android:noHistory="true"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateAlwaysHidden|adjustNothing"
android:theme="@style/Theme.AppCompat.Light.NoActionBar" />
Run Code Online (Sandbox Code Playgroud) 我有一个EditText在我的Activity.一旦Activity开始我就强行打开软键盘.
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
Run Code Online (Sandbox Code Playgroud)
现在,如果软键盘打开,我按下主页按钮,它仍保持打开状态.如何在家用印刷机上强行关闭它?
问题:
我想在按下"添加"按钮时隐藏键盘.
EditText屏幕上有两个.键盘在启动活动时不会出现,这很好,但单击按钮时不会消失.

以下是Stack Overflow的所有可能的问题,我看到他的答案对我没有帮助:
如何在EditText外单击后在android上隐藏软键盘?
和许多其他人.
这是我的代码:
AddActivity
public class AddActivity extends ActionBarActivity {
EditText text1,text2;
DbHelper db;
ListView l;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
db = new DbHelper(this);
l = (ListView) findViewById(R.id.listInAddActivity);
text1 = (EditText) findViewById(R.id.i1);
text2 = (EditText) findViewById(R.id.i2);
// text1.setInputType(InputType.TYPE_NULL);
// text2.setInputType(InputType.TYPE_NULL);
hideKeyboard();
loadDataInAdd();
}
public void addNewTask(View view) {
String s1 = text1.getText().toString();
String s2 = text2.getText().toString();
db.addData(s1,s2);
loadDataInAdd();
hideKeyboard();
}
public void loadDataInAdd()
{
try {
Cursor cursor …Run Code Online (Sandbox Code Playgroud) 有没有办法立即显示或隐藏软键盘,没有任何动画?在Cyanogenmod 10中,它以褪色动画显示.
我用我的类扩展了AlertDialog,显示了我的XML布局.我没有使用AlertDialog的标准按钮,我有自己的OK和Cancel按钮.监听他们的电话dismiss().问题是如果我正在编辑EditText的内容然后按下OK(这是一个Android 3.1平板电脑,键盘不会阻止我与对话框交互),对话框将隐藏但键盘不会,它将保留在后台.可能是什么原因以及如何解决?
这是我的对话框的构造函数,给出了这个想法:
public NetworkCameraParametersDialog(Context context ) {
super(context);
View content = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.dialog, null);
setView(content);
Button btnOk = (Button) content.findViewById(R.id.btn_Ok);
btnOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Some work
dismiss();
}
});
Button btnClose = (Button) content.findViewById(R.id.btn_Close);
btnClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
}
Run Code Online (Sandbox Code Playgroud)