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

时间:2018-07-30 06:22:02

标签: java android fonts styles

代码

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,但不接受...我该怎么办?

3 个答案:

答案 0 :(得分:2)

  

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

您需要根据从setTypeface获得的价值,在buttonSharedPreferences

示例代码

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

答案 1 :(得分:0)

尝试一下。

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

然后将其用作普通共享首选项

答案 2 :(得分:0)

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

}

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