本文介绍了带麦克风和放大器的Andr​​oid搜索栏;话筒事件。 Android的搜索查看带麦克风的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用我的应用程序搜索查看。我想获得在搜索查看文本框中键入的文本,并在另一个TextView中显示。

I am using a searchview in my application. I want to get the text typed in the searchview textbox and display it on another textview.

我能做到这一点使用searchviewListener。

I can do that using searchviewListener.

searchView.setOnQueryTextListener(new android.support.v7.widget.SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            //adapter.filterData(query);
            //display text where ever i want

            return false;
        }

        @Override
        public boolean onQueryTextChange(String query) {
            adapter.filterData(query);
            //display text where ever i want
            return false;
        }
    });

    return true;
}

现在我使用所取得的serachView带麦克风

Now i made the serachView with Mic using

android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"

麦克风被显示和点击。完美..

Mic is displaying and clickable. Perfect..

现在我的问题是如何捕捉这个MIC数据和显示文​​本在以往我想要的。

Now My question is how to capture this MIC data and display text where ever i want.

推荐答案

要显示它是从这里MIC捕获的数据我想给的答案有相同的。

To display data which is captured from the MIC here i tried to give answer for same with the help of android developer link.

正如你在问题中提到,
*麦克风被显示和点击。 Perfect..how捕捉到了这个MIC数据和显示文​​本在以往我想要的。 *

您需要给为Seachable和放大器意图过滤器; XML资源为相同的。

You need to give intent filter for the Seachable & xml resource for the same.

要获得它是捕捉由MIC的数据(这将是一个对话框打开)比你需要检查的意图动作后处理它在 onNewIntent 活动的 Intent.ACTION_SEARCH

To get the data which is capture by mic (which will be open as a Dialog) than you need to handle it in onNewIntent of the activity after checking intent action Intent.ACTION_SEARCH

search_voice.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <android.support.v7.widget.Toolbar
        android:id="@+id/ab_tool"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#00FFFFFF"
        android:minHeight="?attr/actionBarSize">

    </android.support.v7.widget.Toolbar>
    <TextView
        android:id="@+id/txtVoiceSeachQuery"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FF0000"
        android:textSize="26sp" />
</LinearLayout>

SearchVoiceTest

public class SearchVoiceTest extends AppCompatActivity {
    private TextView txtVoiceSeachQuery;
    private SearchView searchView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_voice);
        Toolbar toolbar = (Toolbar) findViewById(R.id.ab_tool);
        setSupportActionBar(toolbar);
        toolbar.setTitle("Voice Text Display");
        txtVoiceSeachQuery = (TextView) findViewById(R.id.txtVoiceSeachQuery);
    }


    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);
            searchView.setQuery(String.valueOf(query), false);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_home_screen, menu);
        searchView =
                (SearchView) menu.findItem(R.id.search_view).getActionView();
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setOnQueryTextListener(new android.support.v7.widget.SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                //adapter.filterData(query);
                //display text where ever i want
                return false;
            }

            @Override
            public boolean onQueryTextChange(String query) {
                //display text where ever i want
                if (txtVoiceSeachQuery != null) {
                    txtVoiceSeachQuery.setText(query);
                }
                return false;
            }
        });

        return true;
    }
}

活动的Andr​​oidManifest Declaratin

 <activity
            android:name="com.example.search.voice.SearchVoiceTest"
            android:configChanges="orientation|screenSize"
            android:launchMode="singleTop" android:label="Search Voice Test"
            android:theme="@style/AppThemeSliderToolbar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.intent.action.SEARCH" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <meta-data
                android:name="android.app.searchable"
                android:resource="@xml/search_with_voice" />


        </activity>

RES / XML / search_with_voice.xml

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:hint="Give Your Query Here"
    android:label="@string/app_name"
    android:voiceSearchMode="showVoiceSearchButton|launchRecognizer">
</searchable>

RES /菜单/ menu_home_screen.xml

<menu 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"
    >
    <item
        android:id="@+id/search_view"
        android:title="Search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="ifRoom|collapseActionView"></item>
</menu>

指定查询seachview我们可以在 onQueryTextChange 获得回调 setOnQueryTextListener 所以从那里测试的目的值分配给后TextView中有你可以让你的改变。

After assigning query to seachview we can get callback setOnQueryTextListener in onQueryTextChange so from there for test purpose value assigned to TextView there you can make your changes.

在这里,我给我的答案的输出环节。

Here i have given link of output of my answer.




  • Search with voice
  • Display Captured data from mic

让我知道什么?

这篇关于带麦克风和放大器的Andr​​oid搜索栏;话筒事件。 Android的搜索查看带麦克风的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 17:08