Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在EditText中添加两个drawableRight?_Android_Android Edittext_Android Drawable - Fatal编程技术网

Android 如何在EditText中添加两个drawableRight?

Android 如何在EditText中添加两个drawableRight?,android,android-edittext,android-drawable,Android,Android Edittext,Android Drawable,我能够在android中轻松地在EditText中添加一个drawableRight,并且在一个drawableRight的情况下,点击事件可以完美地工作。但是我需要两个drawableRight在EditText中 那么,如何在EditText中添加两个drawableRight?我还需要分别在drawableRight上执行单击事件。例如,我想在EditText中添加黄色星号,如下图所示,单击最右侧的图像,我想打开手机的联系人簿,单击黄色星号,我想拨打用户最喜爱的号码列表。 那么我该怎么做呢

我能够在android中轻松地在
EditText
中添加一个
drawableRight
,并且在一个
drawableRight
的情况下,点击事件可以完美地工作。但是我需要两个
drawableRight
EditText

那么,如何在
EditText
中添加两个
drawableRight
?我还需要分别在
drawableRight
上执行单击事件。

例如,我想在
EditText
中添加黄色星号,如下图所示,单击最右侧的图像,我想打开手机的联系人簿,单击黄色星号,我想拨打用户最喜爱的号码列表。

那么我该怎么做呢?有什么想法吗


你不能。
TextView
只能在其两侧包含一个可绘制视图。您唯一的选择是:

  • 创建自定义
    视图
  • 取一些
    ViewGroup
    子体(
    RelativeLayout
    /
    FrameLayout
    /etc),将文本视图与两个
    ImageView
    放在其中

  • 你不能。
    TextView
    只能在其两侧包含一个可绘制视图。您唯一的选择是:

  • 创建自定义
    视图
  • 取一些
    ViewGroup
    子体(
    RelativeLayout
    /
    FrameLayout
    /etc),将文本视图与两个
    ImageView
    放在其中

  • 没有对此的本机支持,因此您在此处有两个选项:

    简单解决方案:创建一个线性布局,在右侧有两个图像视图,这些视图将是您的可绘制视图


    艰难之路:扩展一个
    Drawable
    类,并实现您自己的
    onDraw
    方法,在这里您将绘制两个Drawable。然后将其用于文本视图。

    本机不支持此功能,因此您可以在此处选择两个选项:

    简单解决方案:创建一个线性布局,在右侧有两个图像视图,这些视图将是您的可绘制视图


    艰难之路:扩展一个
    Drawable
    类,并实现您自己的
    onDraw
    方法,在这里您将绘制两个Drawable。而不是将其用于文本视图。

    只需使用RelativeLayout将两个可绘图项放入EditText即可。 要设置内部填充,请将不可见的drawableRight放入EditText:

    /res/values/dimens.xml

    <resources>
        <dimen name="iconSize">32dp</dimen>
    </resources>
    
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <EditText
            android:id="@+id/editText"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:inputType="textAutoComplete"/>
    
        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="@dimen/iconSize"
            android:layout_height="@dimen/iconSize"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:src="@drawable/ic_action_1"/>
    
        <ImageButton
            android:id="@+id/imageButton2"
            android:layout_width="@dimen/iconSize"
            android:layout_height="@dimen/iconSize"
            android:layout_toLeftOf="@+id/imageButton1"
            android:layout_centerVertical="true"
            android:src="@drawable/ic_action_2"/>
    
    </RelativeLayout>
    

    只需使用RelativeLayout将两个Drawable放入EditText即可。 要设置内部填充,请将不可见的drawableRight放入EditText:

    /res/values/dimens.xml

    <resources>
        <dimen name="iconSize">32dp</dimen>
    </resources>
    
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <EditText
            android:id="@+id/editText"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:inputType="textAutoComplete"/>
    
        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="@dimen/iconSize"
            android:layout_height="@dimen/iconSize"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:src="@drawable/ic_action_1"/>
    
        <ImageButton
            android:id="@+id/imageButton2"
            android:layout_width="@dimen/iconSize"
            android:layout_height="@dimen/iconSize"
            android:layout_toLeftOf="@+id/imageButton1"
            android:layout_centerVertical="true"
            android:src="@drawable/ic_action_2"/>
    
    </RelativeLayout>
    

    您需要使用relativeLayout.use relativeLayout并在relativeLayout下设置一条线,就像您的图像一样。您需要使用relativeLayout.use relativeLayout并在relativeLayout下设置一条线,就像您的图像一样。如果您采取“困难”的方式(实际上并不那么困难),请确保还实施了设置边界,以便为您的图像留出空间两个可绘制文件。虽然如果您希望每个可绘制文件使用不同的单击方法,简单的方法是可行的。如果您采取“困难”的方法-实际上并不那么困难-请确保同时实现设置以使两个可绘制文件都有空间。尽管如果您希望每个可绘制文件使用不同的单击方法,简单的方法是可行的。