共享preference保存文本大小 [英] saving text size in shared preference
本文介绍了共享preference保存文本大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图到保存在共享preference文字大小,一边跑我的应用程序意外停止此项目的code:
包com.Shared preference;进口android.app.Activity;
进口android.content.Shared preferences;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.SeekBar;公共类共享preferenceActivity延伸活动{搜索栏寻求=(搜索栏)findViewById(R.id.seekBar1);
ED1的EditText =(EditText上)findViewById(R.id.editText1);
按钮B =(按钮)findViewById(R.id.button1);
私人最终字符串TEXT_VALUE_KEY =textvalue;
私人最终字符串FONT_SIZE_KEY =字号;
最后弦乐我的preF =preference/ **当第一次创建活动调用。 * /@覆盖
公共无效的onCreate(捆绑savedInstanceState)
{super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
b.setOnClickListener(新View.OnClickListener(){ @覆盖
公共无效的onClick(查看为arg0){共享preferences preF = getShared preferences(我的preF,MODE_PRIVATE);
共享preferences.Editor编辑器= pref.edit();
editor.putFloat(FONT_SIZE_KEY,ed1.getTextSize());
editor.putString(TEXT_VALUE_KEY,ed1.getText()的toString());
editor.commit();}}); 共享preferences preFS = getShared preferences(我的preF,MODE_PRIVATE); 最终浮动字号= prefs.getFloat(FONT_SIZE_KEY,12); seek.setProgress((int)的字体大小); ed1.setText(prefs.getString(TEXT_VALUE_KEY,)); ed1.setTextSize(seek.getProgress()); seek.setOnSeekBarChangeListener(新SeekBar.OnSeekBarChangeListener(){
公共无效onStopTrackingTouch(搜索栏为arg0){// TODO自动生成方法存根 } 公共无效onStartTrackingTouch(搜索栏为arg0){
// TODO自动生成方法存根 } 公共无效onProgressChanged(搜索栏为arg0,ARG1 INT,布尔ARG2){
// TODO自动生成方法存根
ed1.setTextSize(字号);
}
});
}
}
XML文件:
<?XML版本=1.0编码=UTF-8&GT?;< TableLayout的xmlns:机器人=http://schemas.android.com/apk/res/android 机器人:layout_width =FILL_PARENT 机器人:layout_height =FILL_PARENT 机器人:方向=垂直><&的TableRow GT;
<搜索栏 机器人:ID =@ + ID / seekBar1 机器人:layout_width =match_parent 机器人:layout_height =WRAP_CONTENT/>
< /&的TableRow GT;<&的TableRow GT;<的EditText 机器人:ID =@ + ID / editText1 机器人:layout_width =match_parent 机器人:layout_height =150dp 机器人:layout_weight =0.6 机器人:EMS =10>< /&的EditText GT;
< /&的TableRow GT;<&的TableRow GT; <按钮 机器人:ID =@ + ID /按钮1 机器人:layout_width =50dp 机器人:layout_height =50dp 机器人:文字=按钮/>< /&的TableRow GT;< / TableLayout>
解决方案
找到你的的onCreate
法的意见。
公共无效的onCreate(捆绑savedInstanceState)
{
super.onCreate(savedInstanceState); 的setContentView(R.layout.main);
求=(搜索栏)findViewById(R.id.seekBar1);
ED1 =(EditText上)findViewById(R.id.editText1);
B =(按钮)findViewById(R.id.button1);
...
...
}
i am trying out to save the text size in shared preference ,while running the project my application stops unexpectedly here is the code :
package com.SharedPreference;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
public class SharedPreferenceActivity extends Activity
{
SeekBar seek=(SeekBar)findViewById(R.id.seekBar1);
EditText ed1=(EditText)findViewById(R.id.editText1);
Button b=(Button)findViewById(R.id.button1);
private final String TEXT_VALUE_KEY="textvalue";
private final String FONT_SIZE_KEY="fontsize";
final String MyPref="preference";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
SharedPreferences pref=getSharedPreferences(MyPref,MODE_PRIVATE);
SharedPreferences.Editor editor=pref.edit();
editor.putFloat(FONT_SIZE_KEY,ed1.getTextSize());
editor.putString(TEXT_VALUE_KEY, ed1.getText().toString());
editor.commit();
}
});
SharedPreferences prefs=getSharedPreferences(MyPref, MODE_PRIVATE);
final float fontsize=prefs.getFloat(FONT_SIZE_KEY, 12);
seek.setProgress((int) fontsize);
ed1.setText(prefs.getString(TEXT_VALUE_KEY, ""));
ed1.setTextSize(seek.getProgress());
seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
// TODO Auto-generated method stub
ed1.setTextSize(fontsize);
}
});
}
}
XML file:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TableRow >
<SeekBar
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</TableRow>
<TableRow >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_weight="0.6"
android:ems="10" ></EditText>
</TableRow>
<TableRow >
<Button
android:id="@+id/button1"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="Button" />
</TableRow>
</TableLayout>
解决方案
find the views in your onCreate
method.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
seek=(SeekBar)findViewById(R.id.seekBar1);
ed1=(EditText)findViewById(R.id.editText1);
b=(Button)findViewById(R.id.button1);
...
...
}
这篇关于共享preference保存文本大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文