繁体   English   中英

使用共享首选项来记住用户在Android中首选的字体大小

[英]Using Shared preferences to remember the font size that a user prefers in Android

我正在开发一个移动歌曲本android应用程序。 我已启用放大或缩小文本。 我希望应用程序能够记住当用户将一首特定的歌曲关闭到另一首甚至更好时,即使用户关闭该应用程序并再次打开它时,用户喜欢的特定字体大小。 这是我尝试的方法:

public void saveFont(View view){
    SharedPreferences sharedPref = getSharedPreferences("fontsize", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putFloat("fontsize",factor.getInt());
    editor.apply();
}

public void rememberFont(View view){
    SharedPreferences sharedPref = getSharedPreferences("fontsize", Context.MODE_PRIVATE);
    double factor = sharedPref.getString("fontsize","");
    factor.setInt();
}

这是整个课程:

public class SongbookActivity extends AppCompatActivity {
private TextView wordMeaning;
private TextToSpeech convertToSpeech;
ScaleGestureDetector scaleGestureDetector;
public double factor;

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dictionary);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarSongActivity);
    TextView textViewTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
    setSupportActionBar(toolbar);


    Intent intent = getIntent();
    Bundle bundle = intent.getExtras();
    int dictionaryId = bundle.getInt("SONG._ID");
    int id = dictionaryId + 1;


    wordMeaning = (TextView)findViewById(R.id.dictionary);


    String title = bundle.getString("SONG._TITLE");
    String description = bundle.getString("SONG._LYRICS");

    final android.support.v7.app.ActionBar ab = getSupportActionBar();
    ab.setHomeAsUpIndicator(R.drawable.left);
    ab.setTitle(null);
    ab.setDisplayHomeAsUpEnabled(true);


    textViewTitle.setText(title);

    textViewTitle.setSelected(true);
   // textViewTitle.setMovementMethod(new ScrollingMovementMethod());

    wordMeaning.setTextIsSelectable(true);

    registerForContextMenu(wordMeaning);
    wordMeaning.setMovementMethod(new ScrollingMovementMethod());
    wordMeaning.setText(description);
    scaleGestureDetector = new ScaleGestureDetector(this, new simpleOnScaleGestureListener());


    }


//copy text or select
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    //user has long pressed your TextView
    menu.add(0, v.getId(), 0, "Song copied to Clipboard");

    //cast the received View to TextView so that you can get its text
    TextView yourTextView = (TextView) v;

    //place your TextView's text in clipboard
    ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
    clipboard.setText(yourTextView.getText());

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onTouchEvent(MotionEvent event) {


    if (event.getPointerCount() > 1) {
        scaleGestureDetector.onTouchEvent(event);
        return true;
    }
    return false;


}

public void saveFont(View view){
    SharedPreferences sharedPref = getSharedPreferences("fontsize", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putFloat("fontsize",factor.getInt());
    editor.apply();
}

public void rememberFont(View view){
    SharedPreferences sharedPref = getSharedPreferences("fontsize", Context.MODE_PRIVATE);
    double factor = sharedPref.getString("fontsize","");
    factor.setInt();
}

public class simpleOnScaleGestureListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {

    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        // TODO Auto-generated method stub
        float size = wordMeaning.getTextSize();
        Log.d("TextSizeStart", String.valueOf(size));

        float factor = detector.getScaleFactor();
        Log.d("Factor", String.valueOf(factor));

        float product = size*factor;
        Log.d("TextSize", String.valueOf(product));
        wordMeaning.setTextSize(TypedValue.COMPLEX_UNIT_PX, product);

        size = wordMeaning.getTextSize();
        Log.d("TextSizeEnd", String.valueOf(size));
        return true;
    }
}



@Override
public boolean onOptionsItemSelected(MenuItem item) {
    double factor = 1;
    float size = wordMeaning.getTextSize();
    saveFont(View view);
    rememberFont(View view);
    Log.d("TextSizeStart", String.valueOf(size));
    switch (item.getItemId()) {
        case R.id.small_layout:
            factor = 0.5;
            break;
        case R.id.medium_layout:
            factor = 0.9;
            break;
        case R.id.large_layout:
            factor = 1.3;
            break;
        case R.id.xlarge_layout:
            factor = 1.8;
            break;
    }



    Log.d("Factor", String.valueOf(factor));

    double product = size*factor;
    Log.d("TextSize", String.valueOf(product));
    wordMeaning.setTextSize(TypedValue.COMPLEX_UNIT_PX, (float)product);

    size = wordMeaning.getTextSize();
    Log.d("TextSizeEnd", String.valueOf(size));
    return super.onOptionsItemSelected(item);



}

}

尝试调用方法以及方法声明时出现错误。 我对此很菜鸟,所以请给我您可能认为可以帮助我的所有细节,无论它有多微不足道。

在这里,您将获得所有的http://developer.android.com/training/basics/data-storage/shared-preferences.html实际上您是使用int将其存储在float中并尝试获取字符串,因此请尝试以下操作

public void saveFont(View view){
    SharedPreferences sharedPref = getSharedPreferences("fontsize", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putInt("fontsize",factor.getInt());
    editor.commit();
}

从回来

public void rememberFont(View view){
    SharedPreferences sharedPref = getSharedPreferences("fontsize", Context.MODE_PRIVATE);
    int  prevFont = sharedPref.getInt("fontsize",-1);

}

在-1中,您可以设置默认字体

只是一个“灯”,您将如何做到这一点。

public void setVariable(float myFloat) {
    pref = _context.getSharedPreferences("fontsize", Context.MODE_PRIVATE);
    editor = pref.edit();
    editor.putFloat("fontsize", myFloat);
    editor.commit();
}

public float getVariable() {
    pref = _context.getSharedPreferences("fontsize", Context.MODE_PRIVATE);
    return pref.getFloat("fontsize", 5.5/*a default value*/);
}

_context可以是一个属性。

EDIT1 :该类对我来说对任何“保存”问题都适用。 根据需要更改

public class SharedPreferencesManager {

    // Shared Preferences
    private SharedPreferences pref;
    private SharedPreferences.Editor editor;
    private Context _context;

    // Shared pref mode
    private final int PRIVATE_MODE_SHARED_PREF = 0;

    // Shared preferences file name
    private final String PREF_NAME = "blabla";

    /*KEYS para o sharedpreferences*/
    private final String KEY_TO_USE = PREF_NAME + "setFont";


    public SharedPreferencesManager(Context context) {
        this._context = context;
        pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE_SHARED_PREF);
        editor = pref.edit();
    }

    public void setFont(float keySize) {
        editor.putFloat(KEY_TO_USE, keySize);
        editor.commit();
    }

        public boolean getFont() {
        return pref.getFloat(KEY_TO_USE, 15/*your default value is in here (in sp)*/);
    }
}

要使用此功能,只需创建一个对象(传递上下文[活动])并在EditText / TextView的每次调用/充气中使用GET方法,然后像这样设置大小

myTextView.setTextSize(mSharedPreferencesManagerObject.getVariable())

因此,在每次缩放更改时,都需要致电

mSharedPreference.setVariable(sizeHere)

它必须工作。 如果不是,则问题出在您的“ OnZoomChange”逻辑/语义上。

如果您在使用SharedPreferences时遇到问题,请尝试使用this 这极大地简化了整个过程,并在内部使用SharedPreferences。 您要做的就是在项目中复制源文件并使用它。

这是一个例子:

在您活动的onCreate()方法中,初始化TinyDB。

TinyDB tinyDB = new TinyDB(this);

然后像这样使用它:

tinyDB.putString("fontSize", "12");

String fontSize = tinyDB.getString("fontSize");

就如此容易。 有许多方法在日常开发中非常有用,只需遍历源文件一次即可。 希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM