【问题标题】:how to get an image from gallery or take from camera and insert it into an edittext?如何从图库中获取图像或从相机中获取图像并将其插入到编辑文本中?
【发布时间】:2015-09-11 00:43:01
【问题描述】:

我是 Android 编程新手。 我想要做的是从图库中获取图像或从相机中获取图像,然后插入可能包含文本的edittext。 (例如,文本....文本...文本.. [图像])

获取图片的代码应该如何插入到edittext的选中光标点?

【问题讨论】:

  • 只能在edittext中设置图片的路径..不是图片
  • 很多年前我就做过这个。我正在开发一个消息应用程序。我所做的是在 EditText 中添加笑脸,那些笑脸图标是图像。
  • 但是我看到了一个允许这种类型操作的应用程序。 youtu.be/_nm06Z3mLJQ

标签: android image camera android-edittext


【解决方案1】:

尝试以编程方式执行此操作:

EditText et_EditImage = (EditText)findViewById(R.id.et_EditImage);
et_EditImage.setCompoundDrawables(null, null, getResources().getDrawable(R.drawable.tick), null);

尝试在 XML 文件中执行此操作:

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

            <EditText
                android:id="@+id/search"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/search_bar"
                android:drawablePadding="8dp"
                android:paddingLeft="30dp"
                android:paddingRight="10dp"
                android:singleLine="true" >
                <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>

我在Here上发现了这个

【讨论】:

  • 非常感谢!我尝试了许多解决方案,它们都奏效了!
  • 我很高兴听到这个。 @YoungJunChoi 接受这个问题,以便将来其他用户可以解决同样的问题。
【解决方案2】:

你可以用 2 种方法做到这一点

1)与SpannableString

 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);
    editText.setText(ss);

2) 与CompoundDrawablesWithIntrinsicBounds

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

【讨论】:

  • 感谢您的帮助!这对我帮助很大!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多