当我重新启动我的应用程序时,尽管该用户更改了字体类型,但它仍然没有更改

时间:2019-03-01 15:02:18

标签: android sharedpreferences font-face typeface

我的应用程序在“设置活动”中有一个部分,用户可以使用共享首选项设置字体。我的MainActivity中有4个viewPage。但是只有3个运作良好。

当用户更改字体时,所有页面都能正常工作。但是,当我重新启动应用程序时,“笔记”标签的文本类型不会改变。但是,如果我单击一个便笺并再次打开列表,则文本类型正在更改用户想要的内容或从“设置活动”中选择的内容。

这是我的SettingActivity:

public class SettingActivity extends AppCompatActivity {

MaterialSpinner selectFontSize, selectFontType;
FButton saveChanges;
TextView fontType, fontSize, text_warning;
SharedPreferences sharedPreferences;
Context context = this;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_setting);
    init();

    sharedPreferences = getApplicationContext().getSharedPreferences(getString(R.string.pref_font_type), MODE_PRIVATE);
    sharedPreferences = getApplicationContext().getSharedPreferences(getString(R.string.pref_font_size), MODE_PRIVATE);

    settingMenu();
    setDefaultTextType();
    setDefaultTextSize();

    changeFontType();
    changeFontSize();

}

private String[] sizeList() {
    return new String[]{getString(R.string.yaziFontAyari),
            "12", "15", "18", "21", "24", "27", "30", "33", "36", "39"};
}

private void changeFontSize() {
    selectFontSize.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener() {
        @Override
        public void onItemSelected(MaterialSpinner view, int position, long id, Object item) {
            float textSize = 12;
            String message = "Karakter boyutu: ";
            if (item.equals("12")) {
                textSize = 12;
                fontType.setTextSize(textSize);
                fontSize.setTextSize(textSize);
                text_warning.setTextSize(textSize);
                Toast.makeText(context, message + String.valueOf(textSize), Toast.LENGTH_SHORT).show();
            } else if (item.equals("15")) {
                textSize = 15;
                fontType.setTextSize(textSize);
                fontSize.setTextSize(textSize);
                text_warning.setTextSize(textSize);
                Toast.makeText(context, message + String.valueOf(textSize), Toast.LENGTH_SHORT).show();
            } else if (item.equals("18")) {
                textSize = 18;
                fontType.setTextSize(textSize);
                fontSize.setTextSize(textSize);
                text_warning.setTextSize(textSize);
                Toast.makeText(context, message + String.valueOf(textSize), Toast.LENGTH_SHORT).show();
            } else if (item.equals("21")) {
                textSize = 21;
                fontType.setTextSize(textSize);
                fontSize.setTextSize(textSize);
                text_warning.setTextSize(textSize);
                Toast.makeText(context, message + String.valueOf(textSize), Toast.LENGTH_SHORT).show();
            } else if (item.equals("24")) {
                textSize = 24;
                fontType.setTextSize(textSize);
                fontSize.setTextSize(textSize);
                text_warning.setTextSize(textSize);
                Toast.makeText(context, message + String.valueOf(textSize), Toast.LENGTH_SHORT).show();
            } else if (item.equals("27")) {
                textSize = 27;
                fontType.setTextSize(textSize);
                fontSize.setTextSize(textSize);
                text_warning.setTextSize(textSize);
                Toast.makeText(context, message + String.valueOf(textSize), Toast.LENGTH_SHORT).show();
            } else if (item.equals("30")) {
                textSize = 30;
                fontType.setTextSize(textSize);
                fontSize.setTextSize(textSize);
                text_warning.setTextSize(textSize);
                Toast.makeText(context, message + String.valueOf(textSize), Toast.LENGTH_SHORT).show();
            } else if (item.equals("33")) {
                textSize = 33;
                fontType.setTextSize(textSize);
                fontSize.setTextSize(textSize);
                text_warning.setTextSize(textSize);
                Toast.makeText(context, message + String.valueOf(textSize), Toast.LENGTH_SHORT).show();
            } else if (item.equals("36")) {
                textSize = 36;
                fontType.setTextSize(textSize);
                fontSize.setTextSize(textSize);
                text_warning.setTextSize(textSize);
                Toast.makeText(context, message + String.valueOf(textSize), Toast.LENGTH_SHORT).show();
            } else if (item.equals("39")) {
                textSize = 39;
                fontType.setTextSize(textSize);
                fontSize.setTextSize(textSize);
                text_warning.setTextSize(textSize);
                Toast.makeText(context, message + String.valueOf(textSize), Toast.LENGTH_SHORT).show();
            }

            if (id >= 0) {
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putFloat(getString(R.string.description_fontSize), textSize);
                editor.commit();
            }
        }
    });
}

private void changeFontType() {
    selectFontType.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener() {
        @Override
        public void onItemSelected(MaterialSpinner view, int position, long id, Object item) {
            String message = "Seçilen Yazı Tipi: ";
            String selected_fontType = "fonts/montserrat.ttf";

            if (item.equals(getString(R.string.ABeeZee))) {
                Toast.makeText(context, message + getString(R.string.ABeeZee), Toast.LENGTH_SHORT).show();
                Calligrapher calligrapher = new Calligrapher(context);
                selected_fontType = "fonts/abeezee.ttf";
                calligrapher.setFont(SettingActivity.this, selected_fontType, true);
            } else if (item.equals(getString(R.string.Armata))) {
                Toast.makeText(context, message + getString(R.string.Armata), Toast.LENGTH_SHORT).show();
                Calligrapher calligrapher = new Calligrapher(context);
                selected_fontType = "fonts/armata.ttf";
                calligrapher.setFont(SettingActivity.this, selected_fontType, true);
            } else if (item.equals(getString(R.string.Duru_Sans))) {
                Toast.makeText(context, message + getString(R.string.Duru_Sans), Toast.LENGTH_SHORT).show();
                Calligrapher calligrapher = new Calligrapher(context);
                selected_fontType = "fonts/duruSans.ttf";
                calligrapher.setFont(SettingActivity.this, selected_fontType, true);
            } else if (item.equals(getString(R.string.Habibi))) {
                Toast.makeText(context, message + getString(R.string.Habibi), Toast.LENGTH_SHORT).show();
                Calligrapher calligrapher = new Calligrapher(context);
                selected_fontType = "fonts/habibi.ttf";
                calligrapher.setFont(SettingActivity.this, selected_fontType, true);
            } else if (item.equals(getString(R.string.Happy_Monkey))) {
                Toast.makeText(context, message + getString(R.string.Happy_Monkey), Toast.LENGTH_SHORT).show();
                Calligrapher calligrapher = new Calligrapher(context);
                selected_fontType = "fonts/happyMonkey.ttf";
                calligrapher.setFont(SettingActivity.this, selected_fontType, true);
            } else if (item.equals(getString(R.string.Kite_One))) {
                Toast.makeText(context, message + getString(R.string.Kite_One), Toast.LENGTH_SHORT).show();
                Calligrapher calligrapher = new Calligrapher(context);
                selected_fontType = "fonts/kitOne.ttf";
                calligrapher.setFont(SettingActivity.this, selected_fontType, true);
            } else if (item.equals(getString(R.string.Maven_Pro))) {
                Toast.makeText(context, message + getString(R.string.Maven_Pro), Toast.LENGTH_SHORT).show();
                Calligrapher calligrapher = new Calligrapher(context);
                selected_fontType = "fonts/mavenPro.ttf";
                calligrapher.setFont(SettingActivity.this, selected_fontType, true);
            } else if (item.equals(getString(R.string.Montserrat_Alternates))) {
                Toast.makeText(context, message + getString(R.string.Montserrat_Alternates), Toast.LENGTH_SHORT).show();
                Calligrapher calligrapher = new Calligrapher(context);
                selected_fontType = "fonts/montserrat.ttf";
                calligrapher.setFont(SettingActivity.this, selected_fontType, true);
            } else if (item.equals(getString(R.string.Offside))) {
                Toast.makeText(context, message + getString(R.string.Offside), Toast.LENGTH_SHORT).show();
                Calligrapher calligrapher = new Calligrapher(context);
                selected_fontType = "fonts/offside.otf";
                calligrapher.setFont(SettingActivity.this, selected_fontType, true);
            }

            if (id >= 0) {
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString(getString(R.string.description_fontType), selected_fontType);
                editor.commit();
            }
        }
    });
}

private void settingMenu() {

    selectFontType.setItems(typeList());

    selectFontSize.setItems(sizeList());

}

private String[] typeList() {
    return new String[]{getString(R.string.yaziTipiSeciniz),
            getString(R.string.ABeeZee),
            getString(R.string.Armata),
            getString(R.string.Duru_Sans),
            getString(R.string.Habibi),
            getString(R.string.Happy_Monkey),
            getString(R.string.Kite_One),
            getString(R.string.Maven_Pro),
            getString(R.string.Offside),
            getString(R.string.Montserrat_Alternates)};
}


private void setDefaultTextType() {
    String default_fontType = "fonts/montserrat.ttf";
    String savedFontType = sharedPreferences.getString(getResources().getString(R.string.pref_font_type), default_fontType);
    new Calligrapher(this).setFont(this, savedFontType, true);

}

private void setDefaultTextSize() {
    float savedFontSize = sharedPreferences.getFloat(getString(R.string.pref_font_size), 15);
    fontSize.setTextSize(savedFontSize);
}

private void init() {
    selectFontSize = findViewById(R.id.select_font_size);
    selectFontType = findViewById(R.id.select_font_type);
    fontSize = findViewById(R.id.font_size);
    fontType = findViewById(R.id.font_type);
    saveChanges = findViewById(R.id.saveChanges);
    text_warning = findViewById(R.id.text_warning);
}

@Override
public void onBackPressed() {
    Toast.makeText(context, "Ayarları kaydetmeniz gerekmektedir!", Toast.LENGTH_SHORT).show();
}

public void saveChanges(View view) {
    Intent goDetailPage = new Intent(this, MainActivity.class);
    startActivity(goDetailPage);
    finish();
}

}

这是我的适配器:

public class NotlarimAdapter extends RecyclerView.Adapter<NotlarimViewHolder> {

private static final int TYPE_HEADER = 0;
private static final int TYPE_CELL = 1;

private ItemClickListener itemClickListener;
private Context context;
private ArrayList<Notes> myNotes;
private LayoutInflater mInflater;
private Database db;
SharedPreferences sharedPreferences;

public NotlarimAdapter(Context context) {
    this.context = context;
    mInflater = LayoutInflater.from(context);
    sharedPreferences = context.getApplicationContext().getSharedPreferences(context.getString(R.string.pref_font_type), MODE_PRIVATE);
    sharedPreferences = context.getApplicationContext().getSharedPreferences(context.getString(R.string.pref_font_size), MODE_PRIVATE);
    refresh();
}

public void refresh() {
     myNotes = new ArrayList<>();
    db = new Database(this.context);
    db.open();
    myNotes = db.getAllNotes();
    db.close();
}

@Override
public int getItemViewType(int position) {
    switch (position) {
        case 0:
            return TYPE_HEADER;
        default:
            return TYPE_CELL;
    }
}

@Override
public int getItemCount() {
    return myNotes.size();
}

@Override
public NotlarimViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = null;

    switch (viewType) {
        case TYPE_HEADER: {
            view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.row_notlarim_big, parent, false);
            return new NotlarimViewHolder(view, itemClickListener) {
            };
        }
        case TYPE_CELL: {
            view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.row_notlarim_small, parent, false);
            return new NotlarimViewHolder(view, itemClickListener) {
            };
        }
    }
    return null;
}


@Override
public void onBindViewHolder(final NotlarimViewHolder holder, final int position) {
    final Notes mNotes = myNotes.get(position);
    switch (getItemViewType(position)) {
        case TYPE_HEADER:
            holder.big_row_title_notlarim.setText(mNotes.getTitle());
            holder.big_row_description_notlarim.setText(mNotes.getDescription());
            String currentDateAndTime = DateFormat.getDateTimeInstance().format(new Date());
            holder.big_row_date_notlarim.setText(currentDateAndTime);

            rowTypeFace(holder.big_row_title_notlarim, holder.big_row_description_notlarim, holder.big_row_date_notlarim);

            holder.setItemClickListener(new ItemClickListener() {
                @Override
                public void onClick(View v, int position, boolean isLongClick) {
                    Toast.makeText(context, mNotes.getTitle(), Toast.LENGTH_SHORT).show();
                    Intent noteDetail = new Intent(context, NoteDetailPage.class);
                    noteDetail.putExtra(Common.NOTE_ID, mNotes.getId());
                    noteDetail.putExtra(Common.NOTE_TITLE, mNotes.getTitle());
                    noteDetail.putExtra(Common.NOTE_DESCRIPTION, mNotes.getDescription());
                    context.startActivity(noteDetail);
                }
            });
            holder.deleteNote_big.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showDeleteDialog(position);
                }
            });
            holder.shareNote_big.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    shareNote(position);
                }
            });
            holder.editNote_big.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    db = new Database(context);
                    db.open();
                    final Dialog dialog = new Dialog(context);
                    dialog.show();
                    dialog.setContentView(R.layout.update_dialog);
                    FButton updateNewNote = dialog.findViewById(R.id.possitiveButton);
                    FButton cancel = dialog.findViewById(R.id.negativeButton);
                    final MaterialEditText newNote = dialog.findViewById(R.id.update_note_edit_text);

                    newNote.setText(holder.big_row_description_notlarim.getText());

                    updateNewNote.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dialog.dismiss();
                            holder.big_row_description_notlarim.setText(newNote.getText());
                            mNotes.setDescription(newNote.getText().toString());
                            db.updateNote(mNotes.getId(),
                                    mNotes.getTitle(),
                                    newNote.getText().toString());
                            db.close();
                        }
                    });
                    cancel.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dialog.dismiss();
                        }
                    });
                }
            });
            break;
        case TYPE_CELL:
            holder.small_row_title_notlarim.setText(mNotes.getTitle());
            holder.small_row_description_notlarim.setText(mNotes.getDescription());
            String currentDateAndTimeSmall = DateFormat.getDateTimeInstance().format(new Date());
            holder.small_row_date_notlarim.setText(currentDateAndTimeSmall);

            rowTypeFace(holder.small_row_title_notlarim, holder.small_row_description_notlarim, holder.small_row_date_notlarim);

            holder.setItemClickListener(new ItemClickListener() {
                @Override
                public void onClick(View v, int position, boolean isLongClick) {
                    Toast.makeText(context, mNotes.getTitle(), Toast.LENGTH_SHORT).show();
                    Intent noteDetail = new Intent(context, NoteDetailPage.class);
                    noteDetail.putExtra(Common.NOTE_ID, mNotes.getId());
                    noteDetail.putExtra(Common.NOTE_TITLE, mNotes.getTitle());
                    noteDetail.putExtra(Common.NOTE_DESCRIPTION, mNotes.getDescription());
                    context.startActivity(noteDetail);
                }
            });
            holder.deleteNote_small.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showDeleteDialog(position);
                }
            });

            holder.shareNote_small.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    shareNote(position);
                }
            });
            holder.editNote_small.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    db = new Database(context);
                    db.open();
                    final Dialog dialog = new Dialog(context);
                    dialog.show();
                    dialog.setContentView(R.layout.update_dialog);
                    FButton updateNewNote = dialog.findViewById(R.id.possitiveButton);
                    FButton cancel = dialog.findViewById(R.id.negativeButton);
                    final MaterialEditText newNote = dialog.findViewById(R.id.update_note_edit_text);

                    newNote.setText(holder.small_row_description_notlarim.getText());

                    updateNewNote.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dialog.dismiss();
                            holder.small_row_description_notlarim.setText(newNote.getText());
                            mNotes.setDescription(newNote.getText().toString());
                            db.updateNote(mNotes.getId(),
                                    mNotes.getTitle(),
                                    mNotes.getDescription());
                            db.close();
                        }
                    });
                    cancel.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dialog.dismiss();
                        }
                    });
                }
            });
            break;
    }
}

private void rowTypeFace(TextView title, TextView description, TextView date) {
    String textType = "fonts/montserrat.ttf";
    String savedTextType = sharedPreferences.getString(context.getResources().getString(R.string.description_fontType), textType);
    Typeface face = Typeface.createFromAsset(context.getAssets(), savedTextType);
    title.setTypeface(face);
    description.setTypeface(face);
    date.setTypeface(face);
}

private void shareNote(int position) {
    Intent shareNoteToFriends = new Intent(Intent.ACTION_SEND);
    shareNoteToFriends.setType("text/plain");
    String shareBody = myNotes.get(position).getTitle();
    String shareMessage = myNotes.get(position).getDescription();
    shareNoteToFriends.putExtra(Intent.EXTRA_SUBJECT, shareBody);
    shareNoteToFriends.putExtra(Intent.EXTRA_TEXT, shareMessage);
    context.startActivity(Intent.createChooser(shareNoteToFriends, "Paylaş"));
}

private void showDeleteDialog(final int position) {
    new AwesomeInfoDialog(context)
            .setTitle("Emin misiniz?")
            .setMessage(myNotes.get(position).getTitle() + " adlı notunuz silinecek!")
            .setColoredCircle(R.color.red)
            .setDialogIconAndColor(R.drawable.ic_dialog_error, R.color.white)
            .setCancelable(true)
            .setColoredCircle(R.color.red)
            .setPositiveButtonbackgroundColor(R.color.red)
            .setPositiveButtonText("Sil")
            .setPositiveButtonTextColor(R.color.white)
            .setNegativeButtonbackgroundColor(R.color.green)
            .setNegativeButtonText("İptal")
            .setNegativeButtonTextColor(R.color.white)
            .setPositiveButtonClick(new Closure() {
                @Override
                public void exec() {
                    db.open();
                    db.deleteNote(myNotes.get(position).getId());
                    db.close();
                    NotlarimAdapter.this.myNotes.remove(position);
                    NotlarimAdapter.this.notifyItemRemoved(position);
                    NotlarimAdapter.this.notifyItemRangeChanged(position, getItemCount() - position);
                }
            })
            .setNegativeButtonClick(new Closure() {
                @Override
                public void exec() {

                }
            })
            .show();
}

@Override
public long getItemId(int position) {
    return position;
}

}

1 个答案:

答案 0 :(得分:0)

首先,您的函数changeFontSize编写不正确。好重

您可以这样做:

private void changeFontSize() {
    selectFontSize.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener() {
        @Override
        public void onItemSelected(MaterialSpinner view, int position, long id, Object item) {

            String message = "Karakter boyutu: ";
            try{
                textSize = Integer.parseInt(item);
            }catch(ParseException e){
                textSize = 12;
            }

            fontType.setTextSize(textSize);
            fontSize.setTextSize(textSize);
            text_warning.setTextSize(textSize);
            Toast.makeText(context, message + String.valueOf(textSize), Toast.LENGTH_SHORT).show();

            if (id >= 0) {
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putFloat(getString(R.string.description_fontSize), textSize);
                editor.commit();
            }
        }
    });
}

您可以对功能changeFontType

执行相同的操作

在这里,您正在覆盖sharedPreferences变量:

sharedPreferences = getApplicationContext().getSharedPreferences(getString(R.string.pref_font_type), MODE_PRIVATE);
sharedPreferences = getApplicationContext().getSharedPreferences(getString(R.string.pref_font_size), MODE_PRIVATE);

我认为您应该阅读sharedPreferences上的documentation才能正确实施。

您应该使用以下命令检索sharedPreferences:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);

然后使用:

读取值
String fontType = sharedPref.getString(R.string.pref_font_type);
String fontSize = sharedPref.getString(R.string.pref_font_size);