使用共享首选项更改字体样式



代码

Button mButton1;
String mDefaultFont1;
SharedPreferences mSharedPreferences1;
SharedPreferences.Editor editor1;
mButton1 = (Button)findViewById(R.id.buttontextfontsent);
mSharedPreferences1 = PreferenceManager.getDefaultSharedPreferences(this);
mDefaultFont1 = mSharedPreferences1.getString("Default_Font1","Normal");
mButton1.setTypeface(mButton1.getTypeface(),Typeface.NORMAL);
mButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String[] list = new String[]{"Normal", "Bold", "Italic", "Bold ITalic"};
AlertDialog.Builder builder = new AlertDialog.Builder(CustomizeFont.this);
builder.setTitle("Make your selection");
builder.setItems(list, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (item==0) mButton1.setTypeface(mButton1.getTypeface(),Typeface.NORMAL);
if (item==1) mButton1.setTypeface(mButton1.getTypeface(),Typeface.BOLD);
if (item==2) mButton1.setTypeface(mButton1.getTypeface(),Typeface.ITALIC);
if (item==3) mButton1.setTypeface(mButton1.getTypeface(),Typeface.BOLD_ITALIC);
editor1 = PreferenceManager.getDefaultSharedPreferences(CustomizeFont.this).edit();
editor1.putString("Default_Font1", String.valueOf(item));
editor1.apply();
}
});
builder.show();
}
});

我可以更改字体,但当我重新启动活动时,它会返回到原始的正常字体。。。问题是因为这条线路mButton1.setTypeface(mButton1.getTypeface(),Typeface.NORMAL);

我需要将TypeFace.NORMAL设置为Default_Font1,但不接受。。。我该怎么办?

我需要将textview设置为Default_Font1,但不接受。。。我该怎么办?

您需要根据从SharedPreferences获得的值在button中使用setTypeface

样本代码

mDefaultFont1 = mSharedPreferences1.getString("Default_Font1","0");
if (mDefaultFont1.equals("0")) {
mButton1.setTypeface(mButton1.getTypeface(), Typeface.NORMAL);
} else if (mDefaultFont1.equals("1")) {
mButton1.setTypeface(mButton1.getTypeface(), Typeface.BOLD);
} else if (mDefaultFont1.equals("2")) {
mButton1.setTypeface(mButton1.getTypeface(), Typeface.ITALIC);
} else if (mDefaultFont1.equals("3")) {
mButton1.setTypeface(mButton1.getTypeface(), Typeface.BOLD_ITALIC);
}
mButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String[] list = new String[]{"Normal", "Bold", "Italic", "Bold ITalic"};
AlertDialog.Builder builder = new AlertDialog.Builder(CustomizeFont.this);
builder.setTitle("Make your selection");
builder.setItems(list, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (item==0) mButton1.setTypeface(null);
if (item==1) mButton1.setTypeface(mButton1.getTypeface(),Typeface.BOLD);
if (item==2) mButton1.setTypeface(mButton1.getTypeface(),Typeface.ITALIC);
if (item==3) mButton1.setTypeface(mButton1.getTypeface(),Typeface.BOLD_ITALIC);
editor1 = PreferenceManager.getDefaultSharedPreferences(CustomizeFont.this).edit();
editor1.putString("Default_Font1", String.valueOf(item));
editor1.apply();
}
});
builder.show();
}
});

试试这个。

editor.putInt("fontCode",mButton1.getTypeFace().getStyle());

然后将其用作正常的共享优先

mDefaultFont1 = mSharedPreferences1.getString("Default_Font1","Normal");
mButton1.setTypeface(mButton1.getTypeface(),Typeface.NORMAL);

这种逻辑是完全错误的,请记住,您将int值保存在首选项(0,1,2,3(中,但首先您将其加载为默认值为"normal"的字符串。

在第二行中,您正在设置按钮的字体,但从同一个按钮中获取当前字体,所以您得到了什么?没有变化。

相反,我建议这样做:

mDefaultFont1 = mSharedPreferences1.getInt("Default_Font1",0);
mButton1.setTypeface(mDefaultFont1);

并保存这样的偏好:

public void onClick(DialogInterface dialog, int item) {
int defaultTypeface = 0;
if (item==0) defaultTypeface = Typeface.NORMAL;
if (item==1) defaultTypeface = Typeface.BOLD;
if (item==2) defaultTypeface = Typeface.ITALIC;
if (item==3) defaultTypeface = Typeface.BOLD_ITALIC;
mButton1.setTypeface(defaultTypeface);
editor1 = PreferenceManager.getDefaultSharedPreferences(CustomizeFont.this).edit();
editor1.putInt("Default_Font1", defaultTypeface);
editor1.apply();

}

注意,我的代码可能有点错误,因为我确实是从内存中写的,没有检查它是否真的很好。

相关内容

  • 没有找到相关文章

最新更新