您的位置:首页 > 移动开发 > Android开发

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);
}
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: