Android の既定の検索ダイアログを起動し、選択した結果行を表示するクリック可能なテキストビューを実装する方法を示す例を探しています。
Android の Google マップ アクション バーの検索フィールドと同じ動作とデザインにする必要があります (例: 左側の拡大鏡アイコン、テキスト ビューが空の場合は「検索」ヒント、クリックすると検索可能フィールドで定義された検索ダイアログが開始されます)。エントリ):
Android の既定の検索ダイアログを起動し、選択した結果行を表示するクリック可能なテキストビューを実装する方法を示す例を探しています。
Android の Google マップ アクション バーの検索フィールドと同じ動作とデザインにする必要があります (例: 左側の拡大鏡アイコン、テキスト ビューが空の場合は「検索」ヒント、クリックすると検索可能フィールドで定義された検索ダイアログが開始されます)。エントリ):
EditText
それは複合ドローアブルのカスタムです
public class SearchEditText extends EditText {
private boolean mMagnifyingGlassShown = true;
private Drawable mMagnifyingGlass;
public SearchEditText(Context context, AttributeSet attrs) {
super(context, attrs);
mMagnifyingGlass = getCompoundDrawables()[0];
}
/**
* Conditionally shows a magnifying glass icon on the left side of the text field
* when the text it empty.
*/
@Override
public boolean onPreDraw() {
boolean emptyText = TextUtils.isEmpty(getText());
if (mMagnifyingGlassShown != emptyText) {
mMagnifyingGlassShown = emptyText;
if (mMagnifyingGlassShown) {
setCompoundDrawables(mMagnifyingGlass, null, null, null);
} else {
setCompoundDrawables(null, null, null, null);
}
return false;
}
return super.onPreDraw();
}
そしてxml
<view
class="com.tr.search.SearchEditText"
android:id="@+id/search_src_text"
android:layout_height="wrap_content"
android:layout_width="0dip"
android:layout_weight="1.0"
android:singleLine="true"
android:ellipsize="end"
android:hint="@string/search_bar_hint"
android:drawableLeft="@drawable/magnifying_glass"
android:freezesText="true"
/>
結果
公式ドキュメントをご覧ください: http://d.android.com/guide/topics/search/search-dialog.html
まず、次のコードで textView をクリック可能にする必要があります
TextView txtView =findViewById(R.id.txtView);
txtView.setOnClickListener(new OnClickListener(View v){
new OnClickListener() {
@Override
public void onClick(View v) {
//Your Code for calling search ativity
}
});
検索アクティビティについては、こちらを参照してください
Eclipseを使用している場合は、Android SDKの検索ダイアログの例も1つあり、クリックするだけです
File->new->android Project->select sdk
->create project from existing project sample
select searchableDictionary