2012-04-16 13 views
0

ボタンを選択すると、値をSDカード(別の方がよいでしょうか?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); 
関連する問題