2012-04-16 2 views
0

버튼을 선택하면 값을 SD 카드 (선호) 또는 다른 XML 레이아웃에 저장할 수 있습니까?Android : .xml 레이아웃의 일부 콘텐츠 (즉, 텍스트 편집)를 저장할 수 있습니까

기본 설정 데이터를 저장할 수있는 공유 환경 설정 (booleans, floats, ints, longs 및 strings)에 대한 내용을 읽었습니다. 나는 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);