Android:如何将十字图标放在自动完成textView的顶部

栏目: Android · 发布时间: 6年前

内容简介:翻译自:https://stackoverflow.com/questions/12576529/android-how-to-put-cross-icon-on-top-of-the-autocomplete-textview
我在将十字按钮放在textview顶部时遇到了问题.我正在使用LinearLayout并且它没有出现,而在Framelayout它工作,但这不能解决我的目的.我附上我的 XML

作为参考,请帮助我克服这个问题.

<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>

谢谢!

在EditText上使用android:drawableLeft属性.

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

如果要动态添加图标,请使用以下命令:

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

要处理点击事件:

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来完成:

Handling click events on a drawable within an EditText

翻译自:https://stackoverflow.com/questions/12576529/android-how-to-put-cross-icon-on-top-of-the-autocomplete-textview


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

爆裂

爆裂

[美] 伊藤穰一、[美] 杰夫·豪 / 张培、吴建英、周卓斌 / 中信出版集团 / 2017-9-1 / 65.00元

越是在发生重大改变的时刻,越是会出现两极分化,赢家、输家有时只在一念间。未来已经装上了全新的操作系统。这是一个重大升级,对我们而言,随之而来的则是陡峭的学习曲线。在指数时代,替换旧逻辑,我们的思维亟需与世界对接,推翻过去已经成为大众所接受的常识,学会差异化思考才能屹立不倒,不被卷入历史的洪流。 在《爆裂》一书中,伊藤穰一和杰夫·豪将这一逻辑提炼为9大原则,帮助人们驾驭这一动荡时刻,应对当下的......一起来看看 《爆裂》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具