Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何通过SharedReferences保存已调整大小的textview?_Android_Sharedpreferences - Fatal编程技术网

Android 如何通过SharedReferences保存已调整大小的textview?

Android 如何通过SharedReferences保存已调整大小的textview?,android,sharedpreferences,Android,Sharedpreferences,通过seekbar I resized textview,是否可以保存已调整大小的textview 使用SharedReferences,并在重新打开活动时显示已调整大小 文本视图 MainActivity.java 公共类MainActivity扩展了AppCompatActivity{ TextView txt; SeekBar skb; Button btn; @Override protected void onCreate(Bundle savedInstanceState) {

通过seekbar I resized textview,是否可以保存已调整大小的textview 使用SharedReferences,并在重新打开活动时显示已调整大小 文本视图

MainActivity.java

公共类MainActivity扩展了AppCompatActivity{

TextView txt;
SeekBar skb;
Button btn;


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


    //SharedPreferences ??





    txt = (TextView) findViewById(R.id.txt);

    skb = (SeekBar) findViewById(R.id.skb);
    skb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            android.view.ViewGroup.LayoutParams layoutParams = txt.getLayoutParams();
            layoutParams.height = progress;
            layoutParams.width = progress;
            txt.setLayoutParams(layoutParams);

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });


    btn = (Button) findViewById(R.id.btn);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            //?????

        }
    });
}
}

活动\u main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.gela.myapplication111.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:background="@color/colorAccent"
        android:id="@+id/txt"
        android:gravity="center"
        android:textStyle="bold" />

    <SeekBar
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:id="@+id/skb"
        android:layout_marginBottom="64dp"
        android:max="250"
        android:indeterminate="false"
        android:layout_above="@+id/btn"
        android:layout_centerHorizontal="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="SAVE"
        android:id="@+id/btn"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="23dp" />
</RelativeLayout>

只需保存新的高度和宽度即可

        preferenceEditor = sharedpreferences.edit();
    preferenceEditor.putString("height",progress);
    preferenceEditor.putString("widht",progress);
    preferenceEditor.commit();
在阅读的时候。。。在onCreate()中


这里有一些例子

用于存储值:

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        android.view.ViewGroup.LayoutParams layoutParams = txt.getLayoutParams();
        layoutParams.height = progress;
        layoutParams.width = progress;
        txt.setLayoutParams(layoutParams);
        SharedPreferences preferences = 
            PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = sharedpreferences.edit()
        editor.putInt("progress", progress).commit();
    }
用于检索和设置:

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

    txt = (TextView) findViewById(R.id.txt);

    SharedPreferences preferences = 
            PreferenceManager.getDefaultSharedPreferences(context);
    int progress = preferences.getInt("progress");

    android.view.ViewGroup.LayoutParams layoutParams = txt.getLayoutParams();
    layoutParams.height = progress;
    layoutParams.width = progress;
    txt.setLayoutParams(layoutParams);

        ...

你在谷歌上搜索过共享参考资料吗?这是安卓系统中最简单的东西之一。。。。哦,还有。你有吗?复制品
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    txt = (TextView) findViewById(R.id.txt);

    SharedPreferences preferences = 
            PreferenceManager.getDefaultSharedPreferences(context);
    int progress = preferences.getInt("progress");

    android.view.ViewGroup.LayoutParams layoutParams = txt.getLayoutParams();
    layoutParams.height = progress;
    layoutParams.width = progress;
    txt.setLayoutParams(layoutParams);

        ...