我正在开发一个移动歌本安卓应用程序。我已启用放大或缩小文本。我希望应用程序能够记住当用户关闭一首特定歌曲时用户喜欢的特定字体大小,甚至当用户关闭应用程序并再次打开它时更好。这是我尝试这样做的方法:
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);
}
}
尝试调用方法和方法声明时出现错误。我是这方面的菜鸟,所以请给我所有你认为可以帮助我的细节,无论它多么微不足道。