字体大小不保存在共享preferences [英] Font size is not saving in SharedPreferences
问题描述
我从一本书存储在文本大小和文本中抽取的样本程序。当我再次运行程序,文字大小正在减小。谁能帮我出我新的Android版
公共无效的onClick(视图v){
//获取共享preference对象
preFS = getShared preferences(prefName,MODE_PRIVATE);
共享preferences.Editor编辑器= prefs.edit();
//保存价值的EditText上以preferences
editor.putFloat(FONT_SIZE_KEY,editText.getTextSize());
editor.putString(TEXT_VALUE_KEY,editText.getText()的toString());
//保存值
editor.commit();
保存//显示文件信息
Toast.makeText(getBaseContext(),字体大小保存成功!,Toast.LENGTH_SHORT).show();
}
});
//装载共享prefernces反对
共享preferences preFS = getShared preferences(prefName,MODE_PRIVATE);
//设置TextView的字体大小为previously保存的值
浮fontSize的= prefs.getFloat(FONT_SIZE_KEY,12);
//初始化搜索栏和的EditText
seekBar.setProgress((INT)fontSize的);
editText.setText(prefs.getString(TEXT_VALUE_KEY,));
editText.setTextSize(seekBar.getProgress());
seekBar.setOnSeekBarChangeListener(新OnSeekBarChangeListener(){
@覆盖
公共无效onProgressChanged(搜索栏为arg0,INT进步,布尔ARG2){
//改变的EditText的字体大小
editText.setTextSize(进度);
}
@覆盖
公共无效onStartTrackingTouch(搜索栏为arg0){
}
@覆盖
公共无效onStopTrackingTouch(搜索栏为arg0){
}
});
您投你的浮动fontSize的
到 INT
当你将它传递给 seekBar.setProgress()
。这使得它失去它的小数部分,并因此变得更小。然后,您检索此修约值从搜索栏
回叫 editText.setTextSize()
它。显然应当变得更小,因为该值已失去了它的小数部分。
尝试设置最大值为你的进步是一个较大的值( seekBar.setMax(10亿)
),调用 seekBar.setProgress((INT )(fontSize的* 10000000))
开始,并在 onProgressChanged
DO editText.setTextSize(((浮动)的进展)/ 10000000)
。我们所做的基本上是重新present一个整数形式的浮点数通过与一个较大的值相乘。见,如果乘以1000浮1.234,你会得到1234.0,它现在可以转换,而不派系的部分损失为int。要将其转换回,我们首先将我们的INT 1234飘起1234.0,再由1000而且,由于在进度条的百分比值的原始程序的工作划分,我们设定的最大值比我们使用转换系数大100倍int和float形式之间的数字。这样,我们preserve程序的原始功能。
I have taken a sample program from a book that stores the text size and text. when I am running the program again, the text size is being decreased. Can anyone help me out I am new to android
public void onClick(View v) {
//Getting the SharedPreference object
prefs = getSharedPreferences(prefName, MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
// save the values in the EditText view to preferences
editor.putFloat(FONT_SIZE_KEY, editText.getTextSize());
editor.putString(TEXT_VALUE_KEY, editText.getText().toString());
// Saves the values
editor.commit();
//Display file saved message
Toast.makeText(getBaseContext(), "Font size saved Successfully!", Toast.LENGTH_SHORT).show();
}
});
// Loading the shared Prefernces object
SharedPreferences prefs = getSharedPreferences(prefName, MODE_PRIVATE);
// Set the TextView font size to the previously saved values
float fontSize = prefs.getFloat(FONT_SIZE_KEY, 12);
// init the SeekBar and EditText
seekBar.setProgress((int)fontSize);
editText.setText(prefs.getString(TEXT_VALUE_KEY, ""));
editText.setTextSize(seekBar.getProgress());
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){
@Override
public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) {
// Change the font size of the EditText
editText.setTextSize(progress);
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
}
@Override
public void onStopTrackingTouch(SeekBar arg0) {
}
});
You cast your float fontSize
to int
when you pass it to seekBar.setProgress()
. This makes it lose its fractional part, and therefore become smaller. Then you retrieve this rounded value back from seekBar
and call editText.setTextSize()
with it. Clearly it should get smaller, as the value has lost its fractional part.
Try setting max value for your progress to be a large value (seekBar.setMax(1000000000)
), call seekBar.setProgress((int)(fontSize * 10000000))
initially, and in onProgressChanged
do editText.setTextSize( ((float) progress) / 10000000 )
. What we do here is basically represent a floating point number in an integer form by multiplying it with a large value. See, if you multiply float 1.234 by 1000, you get 1234.0, which can now be converted to int without any loss of factional part. To convert it back, we first convert our int 1234 to float 1234.0, then divide by 1000. And since your original program worked with percent values in progress bar, we set the maximum value to be 100 times larger than the coefficient we use to convert numbers between int and float forms. This way we preserve the original functionality of your program.
这篇关于字体大小不保存在共享preferences的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!