Android 如何保存编辑文本的颜色、大小、字体?
我有一个编辑文本,我有一个按钮,可以改变颜色,大小和字体,所以当保存为pdf或txt文件的文件,我如何才能得到这3件事与文本我得到的只有文本与正常的黑色风格这是我的代码Android 如何保存编辑文本的颜色、大小、字体?,android,android-edittext,Android,Android Edittext,我有一个编辑文本,我有一个按钮,可以改变颜色,大小和字体,所以当保存为pdf或txt文件的文件,我如何才能得到这3件事与文本我得到的只有文本与正常的黑色风格这是我的代码 size.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { txtSpeechInput.setTextSize(25);
size.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
txtSpeechInput.setTextSize(25);
txtSpeechInput.setTextColor(Color.rgb(150,0,0));
txtSpeechInput.setTypeface(null, Typeface.BOLD);
保存代码:
final File externalStorageDir = new File(
Environment
.getExternalStorageDirectory(), "MyVoiceText");
boolean success = true;
if (!externalStorageDir.exists()) {
success = externalStorageDir.mkdirs();
}
if (success) {
final Dialog dialog2 = new Dialog(MainActivity.this);
dialog2.setContentView(R.layout.dialog_save);
dialog2.show();
editsave = (EditText) dialog2.findViewById(R.id.edit_save);
pdf = (Button) dialog2.findViewById(R.id.pdf_save);
pdf.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
pdf.setEnabled(false);
txt.setEnabled(true);
}
});
txt = (Button) dialog2.findViewById(R.id.txt_save);
txt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
txt.setEnabled(false);
pdf.setEnabled(true);
}
});
dia_save = (Button) dialog2.findViewById(R.id.dia_save);
dia_save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String savename = editsave.getText().toString();
if(!txt.isEnabled()) {
File myFile = new File(externalStorageDir + "/" + savename + ".txt");
File myFile = new File(externalStorageDir + "/" + savename + ".txt");
try {
StringBuffer string = new StringBuffer();
string.append(txtSpeechInput.getText().toString());
Properties properties = new Properties();
// set the properties value
properties.put("text",string.append(txtSpeechInput.getText().toString())); properties.put("textstyle","bold");
properties.put("typeface",txtstyle);
properties.put("etxtColor",txtcolor);
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
myOutWriter.append(string);
myOutWriter.close();
fOut.flush();
fOut.close();
} catch (Exception e) {
}
try {
myFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
dialog2.dismiss();
}else if(!pdf.isEnabled()){
try
{
String savename2 = editsave.getText().toString();
String takeit = txtSpeechInput.getText().toString();
Document document = new Document(PageSize.A4);
Paragraph p = new Paragraph();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(Environment.getExternalStorageDirectory() + "/MyVoiceText/" +savename2+".pdf"));
document.open();
writer.getDirectContent();
p.add(new Phrase(takeit));
document.add(p);
document.close();
Log.d("OK", "done");
} catch (Exception e) {
//LOGGER.error(e.getMessage());
}
重新加载文本文件
File sdcard = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/MyVoiceText/");
//Get the text file
File file = new File(sdcard, String.valueOf(selected));
InputStream input = null;
try {
input = new FileInputStream(file);
Properties prop = new Properties();
// load a properties file
prop.load(input);
}catch (Exception e){
e.printStackTrace();
}
//Read text from file
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
}
br.close();
catch (IOException e) {
//You'll need to add proper error handling here
}
根据您的代码,您似乎正在使用iTextPDF库生成PDF。 使用iTextPDF的字体类创建新字体,并按如下方式使用:
private static Font HelveticaBlue = new Font(Font.FontFamily.HELVETICA, 6,
Font.BOLD, BaseColor.BLUE);
new Paragraph("Blue ColorText"), HelveticaBlue);
我们可以将
属性
添加到包含键和值格式项的文件中
我们可以在文件中编写自己的属性
,也可以检索
如下代码所示:
将属性写入文件:
private void addPropertiesToFile(String mytext){
try {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/CHETAN");
if (!myDir.exists ())
myDir.mkdirs();
String fname = "mytext.txt";
File myFile = new File (myDir, fname);
if (!myFile.exists ())
myFile.createNewFile();
Properties properties = new Properties();
// set the properties value
properties.put("text","dsf");
properties.put("textstyle","bold");
properties.put("typeface","arial");
properties.put("etxtColor","green");
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter =new OutputStreamWriter(fOut);
myOutWriter.write(mytext);
// save properties to project root folder
properties.store(myOutWriter,null);
myOutWriter.close();
fOut.flush();
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}
从文件检索属性:
private void retrievePropertiesFromFile(){
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/CHETAN");
String fname = "mytext.txt";
File myFile = new File (myDir, fname);
InputStream input = null;
try {
input = new FileInputStream(myFile);
Properties prop = new Properties();
// load a properties file
prop.load(input);
// get the property value and print it out
Log.i(getClass().getSimpleName(),prop.getProperty("text"));
Log.i(getClass().getSimpleName(),prop.getProperty("textstyle"));
Log.i(getClass().getSimpleName(),prop.getProperty("typeface"));
Log.i(getClass().getSimpleName(),prop.getProperty("typeface"));
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
更改文本视图的大小、字体和颜色与更改pdf中文本的大小、字体和颜色是不同的。好的,这是我的问题,我如何使用自定义颜色保存文件,…为此,您可以保留文本的所有端口,如颜色、文本、文本样式、,输入一个类,比如TextPojo,并将其保存在服务器或本地数据库或应用程序上下文中。那么有没有代码可以包含这3个类,比如(getText.toString)?保存文件时,我不想编辑任何内容,我想获取颜色或。。。,。。。。。编辑文本,然后保存它,就像它在我的EditTextXt上显示的那样。你是否使用iTextPDF生成PDF?也许你没有得到我的问题,你的代码可以很好地更改所有内容,但我想将所有这些从editText保存到PDFy。你需要保存font_族的值,将font\u size和font\u color设置为size.setOnClickListener()函数中的某些变量,您可以在其中更改textView的颜色。并在将文本写入PDF文档时使用这些值。谢谢,我对代码进行了一些编辑,但这是PDF的主代码。当我关闭应用程序并重新打开它,然后打开文件,字体恢复为正常黑色时,当然我添加了自定义的颜色int,添加properties.store(myOutWriter,null)时的字体和大小;我无法获取文本实际上属性是你文件的额外信息。因此它不应用于你的文本它只是信息你获取此信息并再次应用于你的文本手册检查它,但我猜代码不起作用,因为我给所有编辑文本加色,所以它会将颜色更改为所有新的和加载的文本,所以当我按load时如果在应用程序中选择绿色,则带有蓝色文本的文件的按钮将变为绿色。请参阅我使用的myOutWriter.write(mytext)的代码;将我的文本写入未使用的文件。