Android的:如何把上自动完成TextView的顶部十字图标 [英] Android : How to put cross icon on top of the autocomplete textView

查看:152
本文介绍了Android的:如何把上自动完成TextView的顶部十字图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在推杆上的TextView顶部十字按钮所面临的问题。我使用的LinearLayout,它是不是在这即将到来,而在的FrameLayout它的工作,但是这并没有解决我的目的。我安装我的XML仅供参考,请帮我解决这个问题。

I'm facing problem in putting cross button on top of the textview. I'm using LinearLayout and it is not coming up on that, whereas on Framelayout it work but that does not solve my purpose. I'm attaching my XML for reference, please help me in overcoming this problem.

<LinearLayout
        android:id="@+id/top"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/phone_toolbar"
        android:baselineAligned="true"
        android:gravity="center_horizontal"
        android:paddingBottom="2dp"
        android:paddingTop="2dp" >

        <ImageView
            android:id="@+id/search_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="10dp"
            android:background="@drawable/toolbar_search_icon_phone" >
        </ImageView>

        <AutoCompleteTextView
            android:id="@+id/text"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginRight="10dp"
            android:layout_weight="2"
            android:background="@drawable/toolbar_phone_textfield"
            android:dropDownVerticalOffset="5dp"
            android:ems="10"
            android:focusable="true"
            android:hint="@string/hint"
            android:imeOptions="actionSearch"
            android:paddingLeft="10dp"
            android:paddingRight="20dp"
            android:singleLine="true"
            android:textColor="#000000"
            android:textSize="14sp" />

        <Button
            android:id="@+id/clear_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right|center_vertical"
            android:layout_marginRight="10dip"
            android:background="@drawable/text_clear" />
    </LinearLayout>

感谢您!

推荐答案

使用了android:drawableLeft财产上的EditText

Use the android:drawableLeft property on the EditText.

<EditText
 ...     
android:drawableLeft="@drawable/my_icon" />

如果你想动态添加图标,使用这样的:

if you want to add the icon dynamically, use this:

EditText et = (EditText) findViewById(R.id.myET);
et.setCompoundDrawablesWithIntrinsicBounds(R.drawable.my_icon, 0, 0, 0);

要处理click事件:

To handle the click events:

String value = "";//any text you are pre-filling in the EditText

final EditText et = new EditText(this);
et.setText(value);
final Drawable x = getResources().getDrawable(R.drawable.presence_offline);//your x image, this one from standard android images looks pretty good actually
x.setBounds(0, 0, x.getIntrinsicWidth(), x.getIntrinsicHeight());
et.setCompoundDrawables(null, null, value.equals("") ? null : x, null);
et.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (et.getCompoundDrawables()[2] == null) {
            return false;
        }
        if (event.getAction() != MotionEvent.ACTION_UP) {
            return false;
        }
        if (event.getX() > et.getWidth() - et.getPaddingRight() - x.getIntrinsicWidth()) {
            et.setText("");
            et.setCompoundDrawables(null, null, null, null);
        }
        return false;
    }
});
et.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        et.setCompoundDrawables(null, null, et.getText().toString().equals("") ? null : x, null);
    }

    @Override
    public void afterTextChanged(Editable arg0) {
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }
});

和这也可使用自定义的EditText完成:

and this can also be done using a custom EditText:

<一个href="http://stackoverflow.com/questions/3554377/handling-click-events-on-a-drawable-within-an-edittext">Handling点击在一个EditText

这篇关于Android的:如何把上自动完成TextView的顶部十字图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆