如何使用SharedPreferences来更改字体

时间:2014-02-08 15:57:34

标签: android

通过使用此代码,我想更改textview的字体,但该程序不起作用 我该怎么做?

public class MainActivity extends Activity {
SharedPreferences shared = getSharedPreferences("Prefs", MODE_PRIVATE);
SharedPreferences.Editor editor = shared.edit();
public String fonts = shared.getString("fonts", "BHOMA.ttf");
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    setFace();
}
protected void setFace()
{
    TextView txt1 = (TextView)findViewById(R.id.textView1);
     Typeface face = Typeface.createFromAsset(getAssets(), "font/"+fonts+"");
     txt1.setTypeface(face);
}

}

和prefs.class

公共类Prefs扩展了PreferenceActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

        String fonts = prefs.getString("fonts", "Tahoma.ttf");
        Boolean b = prefs.getBoolean("FIRSTRUN", true);      
}

}

0 个答案:

没有答案