![](/img/trans.png)
[英]How to make the soft keyboard hide when clicked outside of the focused EditText BUT not if another EditText is clicked?
[英]How to hide the soft keyboard in android after doing something outside of edittext which is in ArrayAdapter?
我有一個edittext,當我單擊它時,我得到了所需的數字軟鍵盤。 但是,當我在edittext之外單擊時,我得到了常規鍵盤,但是當用戶在edittext之外單擊時,我需要隱藏鍵盤。 鍵盤也應該在滾動上消失,而不僅僅是在OnClick上消失。
public View getView(int position, View convertView, ViewGroup parent) {
StoreSkuInfo storeSkuInfo = StoreSkuInfoList.get(position);
View row = convertView;
String status="";
StoreSkuHolder holder = null;
if (row ==null) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
row = mInflater.inflate(R.layout.pip_pickupinstore_listcell, null);
holder = new StoreSkuHolder();
holder.qtyText = (EditText)row.findViewById(R.id.pip_qty_et);
holder.qtyLL = (LinearLayout)row.findViewById(R.id.qty_LL);
holder.pipQuantityStatus = (TextView) row.findViewById(R.id.quantity_status);
holder.relativeLayoutStoreInfo = (RelativeLayout) row.findViewById(R.id.RelativeLayoutStoreInfo);
holder.relativeLayoutAisleBay = (RelativeLayout) row.findViewById(R.id.RelativeLayoutAisleBay);
row.setTag(holder);
}else{
holder = (StoreSkuHolder)row.getTag();
}
}
我們知道該getView方法被調用過很多次,因此,在單擊edittext時,將以下代碼放入getView方法中會使鍵盤每秒上下移動。
holder.qtyText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if(hasFocus) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
}
if(!hasFocus) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
// imm.hideSoftInputFromWindow(v.getWindowToken(), 1);
}
}
});
如何解決?
編輯:嘗試了Shruti的答案,如下所示。 我將以下代碼放在父類中:
public static void hideSoftKeyboard(Activity activity)
{
if(activity!= null && activity.getCurrentFocus() != null && activity.getCurrentFocus().getWindowToken() != null)
{
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
}
然后在OnCreate中,我將:
findViewById(android.R.id.content).getRootView().setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.d(TAG, "In setOnTouchListener of root");
hideSoftKeyboard(PIPActivity.this);
return false;
}
});
但是代碼甚至都沒有進入這個TouchListener中。
編輯2我嘗試了Shashank Agarwal的答案。 但是他的答案是特定於布局的。 如果用戶單擊其他任何地方,我需要鍵盤消失。 下面是要澄清的圖片。
下面是EDITTEXT的XML布局。 我從中刪除了一些行,以嘗試保持其正確性。 討論中的EditText位於此底部: android:id =“ @ + id / pip_qty_et”
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/shiptostore_cell"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:background="@color/ce"
android:onClick="onClick"
android:clickable="true"
android:focusable="true"
android:orientation="horizontal" >
<RelativeLayout
android:id="@+id/pip_store_cell_rl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:layout_marginBottom="20dip"
android:gravity="center"
android:paddingBottom="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp" >
<com.HDTextView
android:id="@+id/pickupinstore_availtoday"
style="@style/Normal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:layout_below="@+id/pickupinstore_milesaway"
android:text="Available for Pick Up TODAY"
android:textColor="@color/C7" />
<RelativeLayout
android:id="@+id/RelativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/pickupinstore_availtoday"
android:layout_marginTop="15dip" >
<com.HDTextView
android:id="@+id/seperator"
android:layout_width="0dip"
android:layout_height="0dip"
android:layout_centerInParent="true" />
<RelativeLayout
android:id="@+id/RelativeLayoutAisleBay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="3dp"
android:layout_toLeftOf="@id/seperator"
android:visibility="gone" >
<com.HDTextView
android:id="@+id/pickupinstore_bin"
style="@style/Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="8dip"
android:layout_toRightOf="@+id/pickupinstore_aisle"
android:maxWidth="70dp"
android:singleLine="true"
android:text="Bay [x]"
android:textColor="@color/RGB51" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dip"
android:layout_toRightOf="@id/storeLocationButton"
android:gravity="center" >
<com.HDTextView
android:id="@+id/quantity_status"
style="@style/SmallBold"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_gravity="center_vertical"
android:layout_marginRight="5dp"
singleLine="true"
android:background="@drawable/bevelededge_mob_lightest_green"
android:gravity="center"
android:minWidth="20dp"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:text="99"
android:textColor="@color/White"
android:textSize="12dip" />
</RelativeLayout>
</RelativeLayout>
<LinearLayout
android:id="@+id/qty_LL"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:orientation="vertical"
android:clickable="true"
android:focusable="true"
android:layout_alignParentRight="true">
<com.HDTextView
style="@style/Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:singleLine="true"
android:text="QTY"
android:textColor="@color/Grey" />
<EditText
android:id="@+id/pip_qty_et"
android:layout_width="45dip"
android:layout_height="wrap_content"
android:background="@null"
android:layout_alignParentRight="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:imeOptions="actionDone"
android:gravity="center"
android:hint="5"
android:onClick="test"
android:inputType="number"
android:maxLength="3" />
</LinearLayout>"
<RelativeLayout
android:id="@+id/RelativeLayoutStoreInfo"
android:layout_width="150dip"
android:layout_height="wrap_content"
android:layout_above="@+id/RelativeLayout"
android:layout_alignLeft="@+id/pickupinstore_storename"
android:layout_alignParentTop="true">
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
樣本布局xml文件。
<RelativeLayout android:id="@+id/rootLayout"...
<Textview.../>
<Button../>
....
...
<RelativeLayout/>
在上面的示例中,相對布局是rootLayout。
rootLayout是指該布局的父布局。
您的情況是LinearLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/shiptostore_cell"..../>
嘗試這個 :
public static void hideSoftKeyboard(Activity activity)
{
if(activity!= null && activity.getCurrentFocus() != null && activity.getCurrentFocus().getWindowToken() != null)
{
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
}
只需觸摸根目錄布局即可調用此函數,它將起作用。
rootLayout.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
hideSoftKeyboard(QuestionTemplateActivity.this);
return false;
}
});
將此代碼放在onCreate之外
public void onClick(View view) {
InputMethodManager imm = (InputMethodManager) view.getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
在布局xml文件中,將此行放在父布局上,因此可以單擊
android:onClick="onClick"
我知道為時已晚,它可能對您沒有幫助,但這對其他人有所幫助。 如果要隱藏默認鍵盤,則可以在EditText的XML文件中添加以下行。
android:keyboardNavigationCluster =“ false”
這將起作用:
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
View v = getCurrentFocus();
boolean ret = super.dispatchTouchEvent(event);
if (v instanceof EditText) {
View w = getCurrentFocus();
int scrcoords[] = new int[2];
w.getLocationOnScreen(scrcoords);
float x = event.getRawX() + w.getLeft() - scrcoords[0];
float y = event.getRawY() + w.getTop() - scrcoords[1];
Log.d("Activity",
"Touch event " + event.getRawX() + "," + event.getRawY()
+ " " + x + "," + y + " rect " + w.getLeft() + ","
+ w.getTop() + "," + w.getRight() + ","
+ w.getBottom() + " coords " + scrcoords[0] + ","
+ scrcoords[1]);
if (event.getAction() == MotionEvent.ACTION_UP
&& (x < w.getLeft() || x >= w.getRight() || y < w.getTop() || y > w
.getBottom())) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindow().getCurrentFocus()
.getWindowToken(), 0);
}
}
return ret;
}
這就是我所做的。 之所以可行,是因為我需要鍵盤在單擊或滾動時隱藏自身。
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (getCurrentFocus() != null) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
return super.dispatchTouchEvent(ev);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.