【问题标题】:How can I add an image on EditText如何在 EditText 上添加图像
【发布时间】:2011-04-11 19:45:51
【问题描述】:

我想在EditText 中动态添加图片。可能吗?

如果有人知道,请提供示例代码。

【问题讨论】:

  • 定义“在EditText添加图片”

标签: android image android-edittext


【解决方案1】:

如果是这样的:

就是你说的,那你要么在xml中设置Drawable{Right | Left | Top | Bottom}属性,要么调用对应的java命令。

EditText text = (EditText)findViewById(R.id.text);
text.setCompoundDrawables(null, null, getResources().getDrawable(R.drawable.check_box), null);

【讨论】:

  • java java命令应该调用setBounds(Rect),否则不能出现图片
  • 您已使用可绘制的图像显示,但如果有可能从图库中选择图像到编辑文本视图中,我需要显示图像抛出图库图像。
  • 只需获取图像并将其放入 BitmapDrawable。
  • 好的,我所做的只是图像没有显示在编辑文本字段中。
  • 之后你是不是作废了?
【解决方案2】:

使用框架布局很容易克服这个问题。这里的另一个优点是您可以为按钮提供点击事件。如果您使用 setCompoundDrawables 设置图标,则无法提供点击事件。我在我的项目中实现了搜索栏有删除和搜索图标。

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

            <EditText
                android:id="@+id/search"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/search_bar"
                android:drawablePadding="8dp"
                android:paddingLeft="30dp"
                android:paddingRight="10dp"
                android:inputType="text"
                android:maxLines="1">

                <requestFocus />
            </EditText>

            <Button
                android:id="@+id/searchBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="left|center_vertical"
                android:layout_margin="10dp"
                android:background="@drawable/icon_magnify" />

            <Button
                android:id="@+id/delete"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right|center_vertical"
                android:layout_margin="8dp"
                android:background="@drawable/icon_remove" />
        </FrameLayout>

【讨论】:

  • 如何给按钮添加点击事件?
  • findViewById(R.id.delete).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO 自动生成的方法存根 } });跨度>
  • 能否提供可绘制的图片!!
  • 使用android:inputType="text" android:maxLines="1"并从EditText中删除singleLine
【解决方案3】:

使用android:drawableRightandroid:drawableLeft (取决于你喜欢在哪里对齐图像)

<EditText 
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/searchedTxt"
        android:width="200dp"
        android:layout_alignParentRight="true"
        android:drawableRight="@android:drawable/ic_menu_search"
        android:drawablePadding="@dimen/dimen_10dp"
        />  

【讨论】:

  • 这家伙应该获得奖牌。谢谢老兄!
  • 在最新的 android studio 2.2.3 中,建议使用 android:drawableEnd 而不是 android:drawableRight
【解决方案4】:

你也可以试试这个

    SpannableString ss = new SpannableString("abc\n");
    Drawable d = img.getDrawable();
    d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
    ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
    ss.setSpan(span, 0, 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
    editT.setText(ss);

【讨论】:

  • +1 如果您想在编辑文本中动态添加多个可绘制对象,此代码会很有帮助。例如聊天输入编辑文本。用户可以选择笑脸图标。需要将此 Spanable 字符串附加到可编辑项。
  • setSpan中的3参数从何而来?
  • 字符串“abc”的长度
  • 嗨,在我的情况下,它看起来像这样link。知道为什么吗?我像这样下载drawable:thiss.getResources().getDrawable(R.drawable.friends)
【解决方案5】:

您可以使用setCompoundDrawablesWithIntrinsicBounds (int left, int top, int right, int bottom)

    EditText text = (EditText) findViewById(R.id.edtTest);
    text.setText("Test");
    text.setCompoundDrawablesWithIntrinsicBounds(null, null,
                       getResources().getDrawable(R.drawable.myDrawable, null), null);

【讨论】:

    【解决方案6】:

    您可以通过android:background="@drawable/img" 将图像添加到您的EditText

    如果您想使用九个补丁或其他方式修改样式,但如果您想在EditText 的左侧添加一个小图像,请考虑使用android:drawableRight="@drawable/icon"

    【讨论】:

      【解决方案7】:

      扩展edittext类并按照你的意愿编写代码

      public void setImage(String imageResource) {
          int position = Selection.getSelectionStart(MyEditText.this
                  .getText());
      
          Spanned e = Html.fromHtml("<img src=\"" + imageResource + "\">",
                  imageGetter, null);
      
          MyEditText.this.getText().insert(position, e);
      }
      

      【讨论】:

      • imageResource 是什么?会是像 /sdcard/images/image1.jpg 这样的图像路径吗?
      【解决方案8】:

      你可以使用:

      editText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.pic_name, 0); 
      

      这里建议:
      Calling setCompoundDrawables() doesn't display the Compound Drawable

      【讨论】:

        【解决方案9】:

        创建EditText的实例

        EditText editText = (EditText) findViewById(R.id.EditText1);
        

        然后创建图像的可绘制实例

        Drawable image = getResources().getDrawable(R.drawable.ic_warning); // API 21 and below.
        

        Drawable image = getDrawable(R.drawable.ic_warning); // API 22 and above.
        

        然后调用setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom);设置图片

        editText.setCompoundDrawablesWithIntrinsicBounds(null, null, image, null); 
        

        以上行将图像设置在EditText的右侧

        【讨论】:

          【解决方案10】:

          如果你在适配器类中,你可以试试这个

              holder.myEditText.setCompoundDrawablesWithIntrinsicBounds(null, null,
                      _context.getResources().getDrawable(R.drawable.ic_launcher),
                      null);
          

          如果你在活动课上可以试试这个

              myEditText.setCompoundDrawablesWithIntrinsicBounds(null, null,
                      getResources().getDrawable(R.drawable.ic_launcher),
                      null);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-04-18
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-02-15
            • 1970-01-01
            相关资源
            最近更新 更多