更改按钮的字体样式

时间:2018-07-30 05:12:14

标签: java android fonts styles runtime-error

代码

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.putInt("Default_Font1", item);
                    editor1.apply();

                }
            });
            builder.show();
        }
    });

我更改样式没有问题,但是没有共享偏好...共享偏好出现错误 enter image description here

该行中的错误-“ mDefaultFont1 = mSharedPreferences1.getString(” Default_Font1“,” Normal“);”

为图片感到抱歉...我的logcat没有显示错误...

我的方法正确吗?如果是这样,那我为什么会收到此错误?有人可以帮我吗?预先感谢

0 个答案:

没有答案