简体   繁体   English

有什么办法可以从EditText中的`drawableRight`图标获取视图?

[英]Is there any way how to get view from `drawableRight` icon in EditText?

I need to touch toolTip pop-up to drawable icon in EditText 我需要触摸toolTip弹出到EditText中的可绘制图标

There is my EditText with drawable icon 我的EditText带有可绘制图标

在此处输入图片说明

I need when user click to question mark toolTip pop-up should be open, like this 用户单击问号工具时需要打开提示弹出窗口,像这样

在此处输入图片说明

But this current implementation I have done not straight forward approach. 但是我目前没有实现这种直接的方法。 I put invisible view on the right side and tracking click on drawable icon and when clicked on icon I set toolTip pop-up on invisible view... So it is not right way as I think. 我将不可见视图放在右侧并跟踪可绘制图标的单击,当单击图标时,我在不可见视图上设置了ToolTip弹出窗口...因此,我认为这不是正确的方法。

But I don't know how to get this drawable question mark icon as a view? 但是我不知道如何将这个可绘制的问号图标作为视图?

What am I doing wrong? 我究竟做错了什么?

Fill free to ask 填空问

Thanks 谢谢

Try this: 尝试这个:

 <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

    <ImageView
        android:id="@+id/questionMark"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        />

</RelativeLayout>

Then you can have an onClickListener on the ImageView. 然后,您可以在ImageView上具有onClickListener。

Hope this helps. 希望这可以帮助。

as you are using TextInputLayout with EditText so you have to use https://github.com/ViHtarb/Tooltip library for tool-tip, and do one more thing add a your Question Mark(?) drawable in right of EditText and apply Below Code for onClick of that drawable: 因为您将TextInputLayoutEditText一起使用,所以必须使用https://github.com/ViHtarb/Tooltip库作为工具提示,并再做一件事在EditText的右边添加一个问号(?)可绘制对象并在下面应用该drawable的onClick代码:

editText.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            final int DRAWABLE_LEFT = 0;
            final int DRAWABLE_TOP = 1;
            final int DRAWABLE_RIGHT = 2;
            final int DRAWABLE_BOTTOM = 3;

            if(event.getAction() == MotionEvent.ACTION_UP) {
                if(event.getRawX() >= (editText.getRight() - editText.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
                    // Run your tool-tip action from here 
                   Tooltip tooltip = new Tooltip.Builder(v)
    .setText("Hello tooltip")
    .show();

                 return true;
                }
            }
            return false;
        }
    });

I did some tricked. 我被骗了 It is ugly but works for me. 这很丑,但对我有用。 You can use one button (as place holder) with 0dp for height and with. 您可以将一个按钮(作为占位符)与0dp一起使用,以表示高度和高度。 Library uses the anchorview for only finding the location of popup. 库使用anchorview仅查找弹出位置。 See how did I below. 请参阅下面的内容。

This is the xml 这是XML

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:errorEnabled="true"
                app:hintTextAppearance="@style/TextInput">

                <android.support.design.widget.TextInputEditText
                    android:id="@+id/et_pn"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/username_pn"
                    android:inputType="number"
                    android:maxLength="18"
                    android:drawableEnd="@drawable/ic_close"
                    app:theme="@style/EditText"/>

            </android.support.design.widget.TextInputLayout>

            <Button
                android:id="@+id/placeholder"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_alignParentEnd="true"
                android:layout_marginEnd="15dp"
                android:layout_centerVertical="true"/>

        </RelativeLayout>

This is the application how to click on drawable button. 这是应用程序如何单击可绘制按钮。

TextInputEditText button;
    private Button placeholder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (TextInputEditText) findViewById(R.id.et_pn);
        placeholder = (Button) findViewById(R.id.placeholder);

        button.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                final int DRAWABLE_LEFT = 0;
                final int DRAWABLE_TOP = 1;
                final int DRAWABLE_RIGHT = 2;
                final int DRAWABLE_BOTTOM = 3;

                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (event.getRawX() >= (button.getRight() - button.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {

                        Tooltip.Builder builder = new Tooltip.Builder(MainActivity.this, placeholder)
                                .setCancelable(true)
                                .setDismissOnClick(true)
                                .setCornerRadius(20f)
                                .setGravity(Gravity.BOTTOM)
                                .setText(R.string.tooltip_hello_world);
                        builder.show();
                        return false;
                    }
                }
                return false;
            }
        });

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM