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

问题描述

我在我的应用程序中创建了搜索视图,现在我想添加语音搜索,我是怎么做的 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;
            }

谁能帮助我

解决方法

**编辑:不要忘记权限:Manifest.permission.RECORD_AUDIO

您可以创建语音意图来捕获语音,然后通过 onActivityResult

回想语音

创建意图

        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
           intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,Locale.getDefault());
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT,"Speak to search");
        try {
           startActivityForResult(intent,123456);
        } catch (ActivityNotFoundException a) {
           //show toast?
        }
     }

然后在 onActivityResult 中,捕获语音并将其传递给您的搜索。

@Override
   protected void onActivityResult(int requestCode,int resultCode,Intent data) {
      super.onActivityResult(requestCode,resultCode,data);
      switch (requestCode) {
         case 123456: {
            if (resultCode == RESULT_OK && data != null) {
               ArrayList result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
               textView.setText(result.get(0));
            }
            break;
         }
      }
   }

可以在此处找到 android 文档:https://developer.android.com/reference/android/speech/SpeechRecognizer

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...