editText里面图和文字混排并编辑以后存里面的文字和图片信息等

这篇博客详细介绍了如何在Android的EditText中实现图文混排,包括文字样式(如加粗、下划线)、文字块操作以及图片插入。通过使用SpannableString类,可以实现文本和图片的组合,同时在保存数据时记录图片的位置。示例代码展示了如何创建自定义样式和插入图片,以及处理文本操作的方法。
摘要由CSDN通过智能技术生成

要求:

1、editText放各种样式的文字,比如下划线、加粗等

解决方法是SpannableString类

2、editText放文字块,并且添加和删除都以块为单位,比如你添加“中国”,删除的时候也要一下删除这两个字

解决方法是:SpannableString类,将文字先转换为bitmap然后添加进SpannableString中

3、editText放图片,要求存数据的时候能存图片并图片在文本中的位置

解决方法是:SpannableString类,将图片命名,插入到editText中的位置,在获取它所有文字信息时根据名字就可获得图片所在的位置。

下面的例子中是查阅了网页很多资料,整理的,希望有用的朋友可以看看。

言归正传,实现步骤:

1、布局文件main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ff333333"
    android:orientation="vertical" >




    
    <EditText
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:singleLine="false"
        android:inputType="none"/>




    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >




        <Button
            android:id="@+id/image_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="图片" />




        <Button
            android:id="@+id/url_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="链接" />




        <Button
            android:id="@+id/color_btn1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="颜色1" />




        <Button
            android:id="@+id/color_btn2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="颜色2" />




        <Button
            android:id="@+id/font_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现图文混排,可以使用一个自定义的 EditText 控件,并在其中添加一个 ImageView 控件来实现。 以下是实现方法: 1.创建一个自定义的带有 ImageView 控件的 EditText 控件。 ```java public class RichEditText extends androidx.appcompat.widget.AppCompatEditText { public RichEditText(Context context) { super(context); } public RichEditText(Context context, AttributeSet attrs) { super(context, attrs); } public RichEditText(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } //插入图片 public void insertImage(Bitmap bitmap) { SpannableString ss = new SpannableString(" "); ImageSpan span = new ImageSpan(getContext(), bitmap); ss.setSpan(span, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); append(ss); } } ``` 2.在布局文件中使用自定义的 EditText 控件。 ```xml <com.example.richedittextdemo.RichEditText android:id="@+id/edit_text" android:layout_width="match_parent" android:layout_height="wrap_content" /> ``` 3.在 Activity 中获取自定义的 EditText 控件,并添加一个点击事件。 ```java public class MainActivity extends AppCompatActivity { private RichEditText editText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = findViewById(R.id.edit_text); editText.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //调用选择图片的方法 chooseImage(); } }); } //选择图片 private void chooseImage() { Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent, 1); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK && requestCode == 1) { try { Uri selectedImage = data.getData(); InputStream inputStream = getContentResolver().openInputStream(selectedImage); Bitmap bitmap = BitmapFactory.decodeStream(inputStream); editText.insertImage(bitmap); } catch (FileNotFoundException e) { e.printStackTrace(); } } } } ``` 4.在 onActivityResult 方法中获取选择的图片,并调用自定义的 EditText 控件的 insertImage 方法,将图片插入到 EditText 中。 以上就是在 Android 中实现 EditText 插入图片并实现图文混排的方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值