2012-04-16 35 views
0

因此,通過選擇一個按鈕可能將值保存到SD卡(最好)或其他XML佈局?Android:是否可以保存.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); 
      } 
     }; 

回答

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); 
相關問題