EditText+Span实现Android版Word的输入文字编辑

EditText+Span实现Android版Word的文字编辑

布局就是几个简单的ImageView

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/edit1"
        android:layout_above="@+id/edit2"
        android:paddingLeft="14dp"
        android:paddingRight="14dp"
        android:background="#fff">

        <EditText
            android:id="@+id/edit_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/edit_back"
            android:gravity="top" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/edit2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="10dp"
        android:layout_alignParentBottom="true">

        <ImageView                             //加粗
            android:id="@+id/editbold"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@mipmap/editbold"/>

        <ImageView                             //斜体
            android:id="@+id/editoblique"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@mipmap/editoblique"/>

        <ImageView                             //放大(这个id写错了)
            android:id="@+id/editcenter"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@mipmap/editbig"/>

        <ImageView                              //变色
            android:id="@+id/editpoint"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@mipmap/editpoint"/>

    </LinearLayout>

在activity中实现

    private ImageView editbold;//加粗
    private ImageView editoblique;//斜体
    private ImageView editcenter;//大字
    private ImageView editpoint;//变色

    //判断imageview是否选中
    private boolean isBold = false;
    private boolean isLean = false;
    private boolean isBig = false;
    private boolean isPoint = false;
    private int start;//edittext开始时的位置
    private int count;//edittext添加的数量


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

        editbold = (ImageView) findViewById(R.id.editbold);
        editoblique = (ImageView) findViewById(R.id.editoblique);
        editcenter = (ImageView) findViewById(R.id.editcenter);
        editpoint = (ImageView) findViewById(R.id.editpoint);

        editbold.setOnClickListener(this);
        editoblique.setOnClickListener(this);
        editcenter.setOnClickListener(this);
        editpoint.setOnClickListener(this);

        //EditText动态监听
        edit_content.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                start = i;
                count = i2;
            }

            @Override
            public void afterTextChanged(Editable editable) {

                editchange(editable);//实现各个imageview的功能
            }
        });
    }

    @Override
    public void onClick(View view) {
               //实现各个imageview的点击功能
            case R.id.editbold:
                if(editbold.getDrawable().getCurrent().getConstantState().
                        equals(this.getResources().getDrawable(R.mipmap.editbold).getConstantState())) {
                    editbold.setImageResource(R.mipmap.touchblod);
                    isBold = true;
                } else {
                    editbold.setImageResource(R.mipmap.editbold);
                    isBold = false;
                }
                break;
            case R.id.editoblique:
                if(editoblique.getDrawable().getCurrent().getConstantState().
                        equals(this.getResources().getDrawable(R.mipmap.editoblique).getConstantState())) {
                    editoblique.setImageResource(R.mipmap.touchoblique);
                    isLean = true;
                } else {
                    editoblique.setImageResource(R.mipmap.editoblique);
                    isLean = false;
                }
                break;
            case R.id.editcenter:
                if(editcenter.getDrawable().getCurrent().getConstantState().
                        equals(this.getResources().getDrawable(R.mipmap.editbig).getConstantState())) {
                    editcenter.setImageResource(R.mipmap.toucheditbig);
                    isBig = true;
                } else {
                    editcenter.setImageResource(R.mipmap.editbig);
                    isBig = false;
                }
                break;
            case R.id.editpoint:
                if(editpoint.getDrawable().getCurrent().getConstantState().
                        equals(this.getResources().getDrawable(R.mipmap.editpoint).getConstantState())) {
                    editpoint.setImageResource(R.mipmap.toucheditpoint);
                    isPoint = true;
                } else {
                    editpoint.setImageResource(R.mipmap.editpoint);
                    isPoint = false;
                }
                break;

        }
    }


    //实现即时文本字体改变
    private void editchange(Editable s) {
        if(isBold) {
            s.setSpan(new StyleSpan(Typeface.BOLD),start,start+count, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        if(isLean) {
            s.setSpan(new StyleSpan(Typeface.ITALIC),start,start+count, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        if(isBig) {
            s.setSpan(new RelativeSizeSpan(2.0f),start,start+count, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        if(isPoint) {
            s.setSpan(new ForegroundColorSpan(Color.BLUE),start,start+count, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值