gpt4 book ai didi

安卓 : How to put cross icon on top of the autocomplete textView

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:07:32 25 4
gpt4 key购买 nike

我在将十字按钮放在 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

关于安卓 : How to put cross icon on top of the autocomplete textView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12576529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com