gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:00:57 25 4
gpt4 key购买 nike

代码

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

我可以更改字体,但是当我重新启动 Activity 时,它会恢复为原始的正常字体...问题是因为这一行 mButton1.setTypeface(mButton1.getTypeface(),Typeface.NORMAL);

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

最佳答案

I need to set the textview to Default_Font1 But not accepting... what should I do?

您需要根据从 SharedPreferences 获取的值在按钮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();
}
});

关于java - 使用共享首选项更改字体样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51588047/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com