这里所有的东西都运行正常,但我想在edittext以外的任何地方点击隐藏键盘......
以下是来源:
screen = this;
sp = new ServiceProxy();
login_account=(Button)findViewById(R.id.login_button);
sign_up=(Button)findViewById(R.id.button_login_sign_up_link);
login_username=(EditText)findViewById(R.id.login_user_name);
login_pass=(EditText)findViewById(R.id.login_user_password);
/* //this is the thing i want. means i want to hide keyboard onclick outside edittext
relative_layout = (RelativeLayout)findViewById(R.id.login_account);
relative_layout.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// Hide Keyboard
InputMethodManager imm = (InputMethodManager) UsernameverifyActivity.screen.getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
return true;
}
});*/
login_account.setOnClickListener(new OnClickListener() {
//private Integer id = 0;
@Override
public void onClick(View v) {
String log_username = login_username.getText().toString();
String log_password = login_pass.getText().toString();
if(log_username.matches("")){
MessageDialog.showMessage("Alert!",
"Please enter Username", LoginAccountActivity.screen);
}
else if(log_password.matches("")){
MessageDialog.showMessage("Alert",
"Please enter Password", LoginAccountActivity.screen);
}
else{
try {
String username = log_username;
String password = log_password;
System.out.println("password is: " + password);
requestFor = "login";
additionalData = new JSONObject();
additionalData.put("username", username);
additionalData.put("password", password);
additionalData.put("is_active", "Y");
} catch (JSONException e) {
e.printStackTrace();
}
hideSoftKeyboard();
PostTask pt = new PostTask();
pt.execute(screen);
pdialog = new ProgressDialog(LoginAccountActivity.this);
pdialog.setCancelable(false);
pdialog.setMessage("Please Wait...");
pdialog.show();
}
}
private void hideSoftKeyboard() {
if(getCurrentFocus()!=null && getCurrentFocus() instanceof EditText){
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(login_username.getWindowToken(), 0);
imm.hideSoftInputFromWindow(login_pass.getWindowToken(), 1);
}
}
});
sign_up.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent(getApplicationContext(),CreateAccountActivity.class);
startActivity(i);
overridePendingTransition(R.anim.animation,R.anim.animation2);
finish();
}
});
}
private class PostTask extends AsyncTask<Activity, String, String>
{
@Override
protected String doInBackground(Activity... params) {
if(requestFor.equals("login"))
return (sp.login(params[0]));
//return true;
else
System.out.println("Else Case" +LoginAccountActivity.screen.requestFor);
return "";
}
protected void onPostExecute(String result) {
if (requestFor.equals("login")) {
if (result.equals("true")) {
if(pdialog != null)
{
pdialog.setCancelable(true);
pdialog.dismiss();
}
Intent i=new Intent(getApplicationContext(),MainActivity.class);
startActivity(i);
startActivity(i);
overridePendingTransition(R.anim.animation,R.anim.animation2);
finish();
}
else{
pdialog.setCancelable(true);
pdialog.dismiss();
MessageDialog.showMessage("Alert",
"Incorrect Username or Password", LoginAccountActivity.screen);
}
}
}
}
更新:
我有以下错误:
Null Pointer Exception in LOG internalPolicy.phonewindowDecorView
Text_chat Activity
上的这一行:
InputMethodManager imm = (InputMethodManager) Text_chat.screen.getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
答案 0 :(得分:1)
clearFocus
方法使用&#34;假&#34;容器可能是一个解决方案
您应该创建一个可以像下面那样聚焦的空视图:
<LinearLayout
android:id="@+id/container"
... >
<LinearLayout
android:layout_width="0dip" android:layout_height="0dip"
android:orientation="vertical"
android:focusableInTouchMode="true" android:focusable="true" />
<EditText
... >
<EditText
... >
</LinearLayout>
然后,当您在布局中单击外部时,将以下代码放在onCreate
方法中:
// init your InputMethodManager outside onCreate method:
InputMethodManager mImm;
//...
// in onCreate:
mImm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
// on click event to hide the keyboard:
((LinearLayout) findViewById(R.id.container)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(edittext1.hasFocus() || edittext2.hasFocus() || ...)
hideSoftKeyboard();
}
});
所以hideSoftKeyboard
方法可能是:
private void hideKeyboard() {
mImm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
getActivity().getCurrentFocus().clearFocus();
}
调用clearFocus
方法后,将焦点重置为第一个视图,其中focusable
属性设置为布局中的true
,并hideSoftInputWindow
第二个参数集0
强制键盘隐藏。因此,EditText
失去焦点,SoftKeyboard
消失
我在这里发现了一个类似的技巧:How to hide soft keyboard on android after clicking outside EditText?(和另一个答案,但我找不到它)并且效果很好!
备选方案: 同时重置焦点
private void hideKeyboard() {
mImm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
// set the focus to the container:
((LinearLayout) findViewById(R.id.container)).requestFocus();
}
如果有帮助,请告诉我。
答案 1 :(得分:0)
try this one...
put it below code in your activity in oncreate method and onpause, onresume.
if you want to hidden your softkeyboard try below lines in oncreate ,onpause,onresume in which are the activity you have to perform hidden keyboard.
it will works..
this.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
put .xml file in edittext properties like
android:focusableInTouchMode="true"
so call your hideSoftKeyboard() method in onCreate() in side..
like
oncreate()
{
hideSoftKeyboard();
}
thank you..