我的问题是,当我点击按钮时,我的文字应该是粗体,如果我再次按下那个按钮,那么文本应该从那里取消。我正在使用这个给定的代码,当我点击按钮然后在这个代码的帮助下然后文本是粗体,但是当我再次按下按钮时,文本不会从那里解开....
boldButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0)
{
if(boldBoolean==false)
{
startTemp = edtTouch.getSelectionStart();
endTemp = edtTouch.getSelectionStart();
temp=edtTouch.getText().toString();
Spannable span=edtTouch.getText();
StyleSpan boldText=new StyleSpan(Typeface.BOLD);
span.setSpan(boldText, startTemp, startTemp, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
span.setSpan(boldText, startTemp, startTemp, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
boldButton.setBackgroundResource(R.drawable.bold_bracket);
boldBoolean=true;
}
else
{
/* Spannable span=edtTouch.getText();
StyleSpan boldText=new StyleSpan(Typeface.DEFAULT.getStyle());
span.setSpan(boldText, startTemp, startTemp, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
boldButton.setBackgroundResource(R.drawable.bold);
boldBoolean=false;*/
}
}
});
如果有人知道,请帮助我......
提前感谢...
答案 0 :(得分:1)
finally i got solution myself by using droidwriter.jar , you can download it from http://code.google.com/p/droid-writer/downloads/detail?name=droidwriter-0.8.jar&can=2&q=
after that i am sharing my code here
XML file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
>
<ToggleButton android:layout_height="wrap_content"
android:id="@+id/bold"
android:textOff="B"
android:textOn="B"
android:layout_width="wrap_content"
/>
<ToggleButton android:layout_height="wrap_content"
android:id="@+id/italic"
android:textOff="I"
android:textOn="I"
android:layout_width="wrap_content"
/>
<hu.scythe.droidwriter.DroidWriterEditText
android:id="@+id/DwEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:inputType="textVisiblePassword|textCapSentences"
android:minLines="10" />
</LinearLayout>
/ *和我的java代码* /
package com.example.editordemo;
import hu.scythe.droidwriter.DroidWriterEditText;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.ToggleButton;
public class MainActivity extends Activity {
ToggleButton bold,italic;
DroidWriterEditText edt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bold=(ToggleButton) findViewById(R.id.bold);
italic=(ToggleButton) findViewById(R.id.italic);
edt=(DroidWriterEditText) findViewById(R.id.DwEdit);
edt.setBoldToggleButton(bold);
edt.setItalicsToggleButton(italic);
}
}
答案 1 :(得分:0)
您好我已经完成了您的解决方案,请尝试此代码
public class MainActivity extends ActionBarActivity implements OnClickListener {
short clickedTime=0;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.text);
findViewById(R.id.button).setOnClickListener(MainActivity.this);
}
private void akash()
{
if(clickedTime==0)
{
SpannableString text = new SpannableString(tv.getText().toString());
text.setSpan(new StyleSpan(Typeface.BOLD),tv.getSelectionStart(),tv.getSelectionEnd(),0);
tv.setText(text, TextView.BufferType.SPANNABLE);
clickedTime=1;
}
else
{
SpannableString text = new SpannableString(tv.getText().toString());
text.setSpan(new StyleSpan(Typeface.NORMAL),tv.getSelectionStart(),tv.getSelectionEnd(),0);
tv.setText(text, TextView.BufferType.SPANNABLE);
clickedTime=0;
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
akash();
}
}