如何实现 Google 像搜索栏一样播放
How implement the Google Play like search bar
我需要在我的应用中实现搜索,它应该如下所示
有人愿意向我解释一下它是怎么做到的吗??提前致谢
您可以为此创建自定义布局。
1.采用相对布局并为其提供圆形边框背景。
2.在左侧添加汉堡包图标。
3.在右侧添加麦克风图标。
4.在中心添加edittext。
然后手动点击汉堡包图标和麦克风图标。
我有一些不完全相同的代码,但您可以相应地编辑和更改它。
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:background="@drawable/editext_border">
<ImageView
android:id="@+id/iv_search_mic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="@drawable/ic_mic_green" />
<EditText
android:id="@+id/ed_home_searchbar"
fontPath="fonts/weblysleekuisl.ttf"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toLeftOf="@+id/iv_search_icon"
android:layout_toRightOf="@+id/iv_search_mic"
android:background="@android:color/transparent"
android:hint="@string/action_search"
android:imeOptions="actionSearch"
android:padding="10dp"
android:singleLine="true"
android:textColor="@color/colorText"
android:textCursorDrawable="@drawable/color_cursor"
android:textSize="@dimen/text_xxsmall" />
<ImageView
android:id="@+id/iv_search_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/ic_search_green" />
</RelativeLayout>
然后处理点击事件,如打开 google 麦克风点击语音搜索
searchMic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
OnSwap.getInstance().trackEvent(TAG, "searchMic", "searchMic Clicked");
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Search");
startActivityForResult(intent, VOICE_INPUT_REQUEST_CODE);
}
});
然后是 onActivityResult 方法
if (requestCode == VOICE_INPUT_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
ArrayList<String> matches = data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
int matchSize = matches.size();
for (int index = 0; index < matchSize; index++) {
Log.i(TAG, String.valueOf(index) + ": " + matches.get(index));
if (index == 0) {
searchbar.setText(matches.get(index));
}
}
} else if (resultCode == PlaceAutocomplete.RESULT_ERROR) {
Status status = PlaceAutocomplete.getStatus(this, data);
// TODO: Handle the error.
Log.i(TAG, status.getStatusMessage());
Snackbar.make(locationTextView, status.getStatusMessage(), Snackbar.LENGTH_LONG).show();
} else if (resultCode == RESULT_CANCELED) {
Snackbar.make(locationTextView, "Canceled", Snackbar.LENGTH_LONG).show();
}
}
我需要在我的应用中实现搜索,它应该如下所示
有人愿意向我解释一下它是怎么做到的吗??提前致谢
您可以为此创建自定义布局。 1.采用相对布局并为其提供圆形边框背景。 2.在左侧添加汉堡包图标。 3.在右侧添加麦克风图标。 4.在中心添加edittext。 然后手动点击汉堡包图标和麦克风图标。
我有一些不完全相同的代码,但您可以相应地编辑和更改它。
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:background="@drawable/editext_border">
<ImageView
android:id="@+id/iv_search_mic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="@drawable/ic_mic_green" />
<EditText
android:id="@+id/ed_home_searchbar"
fontPath="fonts/weblysleekuisl.ttf"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toLeftOf="@+id/iv_search_icon"
android:layout_toRightOf="@+id/iv_search_mic"
android:background="@android:color/transparent"
android:hint="@string/action_search"
android:imeOptions="actionSearch"
android:padding="10dp"
android:singleLine="true"
android:textColor="@color/colorText"
android:textCursorDrawable="@drawable/color_cursor"
android:textSize="@dimen/text_xxsmall" />
<ImageView
android:id="@+id/iv_search_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/ic_search_green" />
</RelativeLayout>
然后处理点击事件,如打开 google 麦克风点击语音搜索
searchMic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
OnSwap.getInstance().trackEvent(TAG, "searchMic", "searchMic Clicked");
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Search");
startActivityForResult(intent, VOICE_INPUT_REQUEST_CODE);
}
});
然后是 onActivityResult 方法
if (requestCode == VOICE_INPUT_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
ArrayList<String> matches = data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
int matchSize = matches.size();
for (int index = 0; index < matchSize; index++) {
Log.i(TAG, String.valueOf(index) + ": " + matches.get(index));
if (index == 0) {
searchbar.setText(matches.get(index));
}
}
} else if (resultCode == PlaceAutocomplete.RESULT_ERROR) {
Status status = PlaceAutocomplete.getStatus(this, data);
// TODO: Handle the error.
Log.i(TAG, status.getStatusMessage());
Snackbar.make(locationTextView, status.getStatusMessage(), Snackbar.LENGTH_LONG).show();
} else if (resultCode == RESULT_CANCELED) {
Snackbar.make(locationTextView, "Canceled", Snackbar.LENGTH_LONG).show();
}
}