0

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

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



}

}

尝试调用方法和方法声明时出现错误。我是这方面的菜鸟,所以请给我所有你认为可以帮助我的细节,无论它多么微不足道。

4

3 回答 3

0

在这里你会得到所有http://developer.android.com/training/basics/data-storage/shared-preferences.html 实际上你用 int 存储在 float 中并试图进入 string 所以试试下面

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 你可以设置你的默认字体

于 2015-10-01T13:39:30.900 回答
0

只是一个“光”你怎么能做到这一点。

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”逻辑/语义中。

于 2015-10-01T13:43:24.987 回答
0

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

这是一个例子:

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

TinyDB tinyDB = new TinyDB(this);

然后像这样使用它:

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

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

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

于 2015-10-01T13:58:30.960 回答