EditText+Span实现Android版Word的输入文字编辑
2018-03-31 20:48
417 查看
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); a102 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); } }
相关文章推荐
- Android 实现限制EditText输入文字的数量
- Android实现限制EditText输入文字的数量
- 转 Android实现限制EditText输入文字的数量
- Android实现限制EditText输入文字的数量
- Android editText.addTextChangedListener实现还有多少文字可以输入
- Android实现限制EditText输入文字的数量
- Android 的EditText实现不可编辑
- Android EditText中输入值以星号*显示 (密码的实现)
- android 实现edittext输入内容后可以点击右侧小图片进行清除内容
- 限制Android的EditText输入文字时换行
- Android中EditText实现不可编辑解决办法
- android TextWatcher 监听textview和edittext的文字输入改变
- android 实现edittext的动态可编辑和不可编辑
- Android:EditText限制文字输入
- Android用户界面 UI组件--TextView及其子类(三) EditView以及各种Span文字样式讲解
- Android中限制EditText(输入框)文字输入长度
- Android用户界面 UI组件--TextView及其子类(三) EditView以及各种Span文字样式讲解
- [android]How to implement an not editable scrollable EditText?(如何实现一个不可编辑的带滚动条的EditText?) .
- Android:EditText限制文字输入