如何在搜索视图中添加语音搜索

问题描述

我在我的应用程序中创建了搜索视图,现在我想添加语音搜索,我是怎么做的 enter image description here 这是图片 在此搜索视图中可用,我想在其中添加语音搜索

这是Activity_main.xml的代码,在它创建的搜索视图中,现在想要添加语音搜索,其中语音搜索的结果必须显示在搜索框中

activity_main.xml

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
    >

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
       >
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="bottom"
            android:orientation="vertical">

            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:padding="10dp"
                android:fontFamily="@font/custom_font"
                android:text="@string/app_name"
                android:textColor="@android:color/background_dark"
                android:textSize="30dp" />

            <android.support.v7.widget.SearchView
                android:id="@+id/search_view"
                android:layout_width="302dp"
                android:layout_height="wrap_content"
                android:layout_margin="12.1dp"
                android:background="@drawable/search_view_design"
                android:clickable="true"
                android:contextClickable="true"
                android:focusable="true"
                android:inputType="text"
                android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"
                app:queryHint="Type Word..">



            </android.support.v7.widget.SearchView>




        </LinearLayout>

    </android.support.v7.widget.Toolbar>

    </android.support.design.widget.AppBarLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:orientation="vertical">

        <RelativeLayout
            android:id="@+id/empty_history"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_marginTop="150dp"
            android:visibility="visible">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Search History Here.."
                android:textAlignment="center"
                android:textColor="#313131"
                android:textSize="22sp"
                android:layout_gravity="center_horizontal" />
        </RelativeLayout>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view_history"
            android:layout_margin="5dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="vertical" />

    </LinearLayout>


</android.support.design.widget.CoordinatorLayout>

MainActicity.java

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolBar = findViewById(R.id.toolBar);
        setSupportActionBar(toolBar);
        textView = findViewById(R.id.textView);
        searchView = findViewById(R.id.search_view);
        searchView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                searchView.onActionViewExpanded();

            }
    });
        mdatabase = new Databasehelper(this);
        if(mdatabase.checkDatabase())
        {
            openDatabase();
        }
        else
        {
            LoadDatabase ld = new LoadDatabase(this);
            ld.execute();
        }

        final String[] from = new String[] {"en_word"};
        final int[] to  = new int[]{R.id.suggestion_text};
        suggestionadapter = new SimpleCursorAdapter(MainActivity.this,R.layout.suggestion_row,null,from,to,0)
        {
            @Override
            public void changeCursor(Cursor cursor) {
                super.swapCursor(cursor);
                }
        };
        searchView.setSuggestionsAdapter(suggestionadapter);
        searchView.setOnSuggestionListener(new SearchView.OnSuggestionListener(){
            @Override
            public boolean onSuggestionSelect(int i) {
                return true;
            }

            @Override
            public boolean onSuggestionClick(int i) {
                CursorAdapter ca = searchView.getSuggestionsAdapter();
                Cursor cursor = ca.getCursor();
                cursor.moveToPosition(i);
                String clicked_word = cursor.getString(cursor.getColumnIndex("en_word"));
                searchView.setQuery(clicked_word,false);
                searchView.clearFocus();
                searchView.setFocusable(false);
                Intent intent = new Intent(MainActivity.this,Word_meaningActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("en_word",clicked_word);
                intent.putExtras(bundle);
                startActivity(intent);
                return true;
            }
        });
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                    String text = searchView.getQuery().toString().replaceAll("\\s+","");
                    Pattern P = Pattern.compile("[A-Za-z \\-.]{1,25}");
                    Matcher m = P.matcher(text);
                if(m.matches()) {
                    Cursor c = mdatabase.getmeaning(text);
                    if (c.getCount() == 0) {

                        showDialog();
                    } else {
                        searchView.clearFocus();
                        searchView.setFocusable(false);

                        Intent intent = new Intent(MainActivity.this,Word_meaningActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putString("en_word",text);
                        intent.putExtras(bundle);
                        startActivity(intent);
                    }
                }

                else{
                    showDialog();
                }

                return false;
            }

谁能帮助我

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)