【问题标题】:open keyboard only when user click into the searchView仅当用户单击 searchView 时才打开键盘
【发布时间】:2021-11-22 06:57:46
【问题描述】:

我正在使用带有 json 的 WordPress 网站制作应用程序,我已将 searchView 添加到我的 MainActivity 中,一切正常,但唯一的问题是当 MainActivity 显示时,searchView 的键盘会自动出现,我希望它在用户点击时显示搜索栏..

这是我显示 searchView 的代码:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bgcoor"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.punjabidharti.punjabistatus.MainActivity"
    tools:showIn="@layout/activity_main">

    <SearchView
        android:id="@+id/searchView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:queryHint="Search Status Here"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_centerHorizontal="true"
        android:iconifiedByDefault="false"
        android:layout_marginTop="10dp"
        android:layout_alignParentTop="true"


    />

<ScrollView
        android:id="@+id/scrollView1"
        android:layout_weight="1"
        android:layout_below="@+id/searchView"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="60dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="10dp"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:padding="10dp">

                <TextView
                    android:id="@+id/hash"
                    android:layout_width="0dp"
                    android:layout_height="100dp"
                    android:layout_weight="1"
                    android:layout_marginRight="5dp"
                    android:text="#Hashtag"
                    android:textSize="30dp"
                    android:textStyle="bold"
                    android:gravity="center"
                    android:textColor="#fff"
                    android:background="@color/colorPrimary"/>

            </LinearLayout>

 </LinearLayout>

</ScrollView>

</RelativeLayout>

这是使它工作的代码:

addListenerOnButton();

 public void addListenerOnButton() {

        final Context context = this;

  SearchView searchview = (SearchView) findViewById(R.id.searchView);
                        searchview.setOnQueryTextListener(new SearchView.OnQueryTextListener(){

                            @Override
                            public boolean onQueryTextSubmit(String s) {

                                // some code here


                                return false;
                            }

                            @Override
                            public boolean onQueryTextChange(String s) {
                                return false;
                            }
                        });

}







 

请帮忙..

【问题讨论】:

  • android:windowSoftInputMode="stateHidden" 将此添加到 AndroidManifest.xml 文件中的 MainAcitivity 中
  • ... 根据您的用例,您可能需要android:windowSoftInputMode="stateHidden|adjustResize" 以允许Android 调整活动大小,使其不被键盘覆盖(如有疑问check the docs)跨度>
  • 不起作用兄弟 :( @Nitish
  • getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)‌​; 尝试在运行时在 onCreate() 方法中添加它,如果仍然无法正常工作,请更新 Manifest 文件的代码 `
  • 那么你将不得不提供更多信息,这是正确的方法,如果键盘仍然弹出,那么也许分享你所有的布局,以及你的活动/片段代码.

标签: android json android-studio search searchview


【解决方案1】:

请尝试以这种方式使用下面的代码在需要时隐藏和显示键盘,当你第一次进入活动时然后隐藏它。

public class KeyboardUtils {

/**
 * this method shows the keyboard on corresponding edittext
 * @param editText
 */
public static void showKeyboard(EditText editText) {
    InputMethodManager imm = (InputMethodManager) TaxiappApplication.getInstance().getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}

/**
 * this method hides the keyboard on corresponding edittext
 * @param editText
 */
public static void hideKeyboard(EditText editText) {
    try {
        InputMethodManager imm = (InputMethodManager) TaxiappApplication.getInstance().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
    }catch (Exception e){
        ExceptionHandler.printStackTrace(e);
    }
}

/**
 * this method hides the keyboard on current view
 *
 * @param activity
 */
public static void hideKeyboard(Activity activity) {

    try {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
        //Find the currently focused view, so we can grab the correct window token from it.
        View view = activity.getCurrentFocus();
        //If no view currently has focus, create a new one, just so we can grab a window token from it
        if (view == null) {

            view = new View(activity);

        }
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);

    }catch (Exception e){
        ExceptionHandler.printStackTrace(e);
    }
}


public static boolean isActiveKeyboard(Activity activity){
    return ((InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE)).isActive();
}

/**
 * this method hides the keyboard on view
 * @param activity
 */
public static void hideKeyboardOnview(Activity activity){
    try {
        activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    }catch (Exception e){
         ExceptionHandler.printStackTrace(e);
    }
}

}

希望对你有帮助

【讨论】:

  • android:focusable="false" android:focusableInTouchMode="false" 这对我有用,谢谢大家的帮助
猜你喜欢
  • 2021-10-21
  • 2015-09-13
  • 2020-11-11
  • 1970-01-01
  • 2020-06-19
  • 2016-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多