寻求改变文字大小

时间:2013-08-26 19:57:57

标签: android user-interface seekbar

我想使用搜索栏更改编辑文本的文本大小,每次关闭活动并重新打开它时,搜索栏会自动增加文本大小,这是代码

package com.e_orthodoxy.orthodox_prayers;

public class WakeupActivity extends Activity {

    private SharedPreferences prefs;
    private SeekBar seekbar;
    private EditText edittext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wakeup);

        seekbar = (SeekBar) findViewById(R.id.seekBar1);
        edittext = (EditText) findViewById(R.id.id_wakeup_txt);

        prefs = getPreferences(MODE_PRIVATE);

        float fs = prefs.getFloat("fontsize", 12);
        seekbar.setProgress((int)fs);
        edittext.setTextSize(seekbar.getProgress());


        seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar seekBar){
                prefs = getPreferences(MODE_PRIVATE);
                SharedPreferences.Editor ed = prefs.edit();
                ed.putFloat("fontsize", edittext.getTextSize());
                ed.commit();    
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar){

            }
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser){
                edittext.setTextSize(progress);
            }
        });


    }


    @Override
    public void onBackPressed() {

        Intent intent_wakeup_back = new Intent (WakeupActivity.this, DailyPrayersActivity.class);
        startActivity(intent_wakeup_back);
        finish();
    }   
}

2 个答案:

答案 0 :(得分:11)

用setTextSize替换setTextSize(float size)(int unit,float size) 并使用TypedValue.COMPLEX_UNIT_PX作为单位

float fs = prefs.getFloat("fontsize", 12);
seekbar.setProgress((int)fs);
//edittext.setTextSize(seekbar.getProgress());
edittext.setTextSize(TypedValue.COMPLEX_UNIT_PX,seekbar.getProgress());

并且不要忘记 onProgressChanged

 //edittext.setTextSize(progress);
 edittext.setTextSize(TypedValue.COMPLEX_UNIT_PX,progress);

答案 1 :(得分:-2)

希望这个答案令您满意。

所有代码都写在onCreate()方法中。并按照类型转换(TextView和SeekBar)

SeekBar seekBar =(SeekBar)findViewById(R.id.seekbar);         SharedPreferences prefs = getPreferences(MODE_PRIVATE);

    float fs = prefs.getFloat("fontsize", 12);
    seekBar.setProgress((int)fs);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,seekBar.getProgress());

    seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
            textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,seekBar.getProgress());
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            SharedPreferences prefs = getPreferences(MODE_PRIVATE);
            SharedPreferences.Editor ed = prefs.edit();
            ed.putFloat("fontsize", textView.getTextSize());
            ed.commit();
        }
    });