Android clickable TextView with hint and image to start search dialog and present selected result

柔情痞子 提交于 2019-12-02 10:09:32

问题


I'm searching for an example that shows how I can implement a clickable textview that starts the Android default search dialog and displays a selected result line.

It should have the same behaviour and design as the search field in the Google Maps action bar on Android (e. g. magnifier glass icon on the left, a "Search" hint if the textview is empty, a click starts the search dialog defined by a searchable entry):


回答1:


That is custom EditText with compound drawable

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();
    }

And 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"
            />

Result




回答2:


Your should look at the official documentation : http://d.android.com/guide/topics/search/search-dialog.html




回答3:


well first you have to make your textView clickable with following code

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
        }
    });

for search activity plz refer here

there is also one example given for search dialog in android sdk if you are using eclipse, then just click on

File->new->android Project->select sdk
->create project from  existing project sample
select searchableDictionary


来源:https://stackoverflow.com/questions/5348763/android-clickable-textview-with-hint-and-image-to-start-search-dialog-and-presen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!