0

因此,通过选择一个按钮可能将值保存到 SD 卡(最好)或另一个 xml 布局?

我已经阅读了一些关于使用共享首选项的内容,您可以在其中保存任何原始数据:布尔值、浮点数、整数、长整数和字符串。我不是 100% 确定我正在寻找正确的区域。

例如...

XML

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Enter weight in lbs:" 
    android:textStyle="bold"/>

<EditText
    android:id="@+id/weight"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inputType="number" />
</LinearLayout>

<LinearLayout    
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">    
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="40 yard dash time (4.20s - 8.50s):"
    android:textStyle="bold" />

<EditText
    android:id="@+id/fourtytime"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal" >
</EditText>
</LinearLayout>

JAVA(请忽略 onCreate 方法中的微调器)

package com.aces.acesfootballuk;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class CoachesPage extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.coachespage);

    Spinner spinner1 = (Spinner) findViewById(R.id.spinnerdowns);
     ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.downs, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner1.setAdapter(adapter);

        Spinner spinner2 = (Spinner) findViewById(R.id.spinnerplayers);
         ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(
                    this, R.array.players, android.R.layout.simple_spinner_item);
            adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner2.setAdapter(adapter2);
            }
        };
4

1 回答 1

0
// declare view in top level of class
private final TextView weightView;

// declare your preferences file name
private static final String PREF_FILE = "data.prefs";

// declare field values in your preferences
private static final String KEY_WEIGHT = "KEY_WEIGHT";

// create a reference the graphics objects in your onCreate method
weightView = (TextView)findViewById(R.id.weight);

// read the data in the textView (do this after some event where you want to read the text)
String weight = weightView.getText().toString();

// write the data to the shared prefs 
SharedPreferences settings = getSharedPreferences(PREF_FILE, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt(KEY_WEIGHT, Integer.parseInt(weight));

// Commit the edit
editor.commit();

// if you want to read the data later
SharedPreferences settings = getSharedPreferences(PREF_FILE, 0);
int defaultWeight = -1;
int retrievedWeight = settings.getInt(KEY_WEIGHT, defaultVWeight);
于 2012-04-16T19:36:46.387 回答