2013-02-12 31 views
3

要求:我需要將用戶標識和密碼的值存儲爲String.xml中的字符串值,這些值是用戶確實需要的。Android:如何通過編碼在Strings.xml中存儲變量的值

問題:據我所見,解決方案是使用共享首選項。雖然我使用共享偏好,價值觀沒有得到存儲在String.xml

這裏,是我的strings.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 

<string name="app_name">Sp</string> 
<string name="hello_world">Hello world!</string> 
<string name="menu_settings">Settings</string> 
<string name="id" ></string> <!-- fill in this value from java code, how ? --> 
<string name="pwd"></string> <!-- fill in this value from java code, how ? --> 

</resources> 

佈局頁:activity_main.xml中

<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" 
tools:context=".MainActivity" > 

<EditText 
    android:id="@+id/text" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="178dp" 
    android:ems="10" 
    android:text="id" > 

    <requestFocus /> 
</EditText> 

<Button 
    android:id="@+id/button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/editText1" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="44dp" 
    android:text="Send" /> 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBottom="@+id/text" 
    android:layout_alignParentLeft="true" 
    android:layout_marginBottom="40dp" 
    android:layout_marginLeft="22dp" 
    android:text="id" /> 



</RelativeLayout> 

EdText。 java的

import android.app.Activity; 

import android.content.SharedPreferences; 

import android.content.SharedPreferences.Editor; 

import android.os.Bundle; 

import android.util.Log; 

import android.view.View; 

import android.widget.Button; 

import android.widget.EditText; 


public class EdText extends Activity { 

    @Override 

    public void onCreate(Bundle savedInstanceState) 

    { 

     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_main); 


    final EditText et=(EditText)findViewById(R.id.text); 


    Button but = (Button) findViewById(R.id.Button); 
    but.setOnClickListener(new View.OnClickListener(){ 

     public void onClick(View arg0) 
     { 
      String s1 = ((EditText)findViewById(R.id.text)).getText().toString().trim(); 

      //Saving your strings 
    SharedPreferences prefs = getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE); 
    Editor editor = PreferenceManager.getDefaultSharedPreferences(Context).edit(); 
     editor.putString("s1", s1.getText().toString()); 
    editor.commit();  



    //retrieving your strings from preferences 
    SharedPreferences prefs2 = getSharedPreferences("myPrefsKey",Context.MODE_PRIVATE); 
    String s12 = prefs.getString("s12", ""); //empty string is the default value 


    } 

    }); 
} } 

謝謝您的時間

+11

'Strings.xml'不能在運行時由應用程序改變。保存到SharedPreferences /數據庫並從那裏讀取。我不知道你是如何得到'SharedPreferences'保存到'Strings.xml'的。 – 2013-02-12 02:19:37

+1

這是不可能的,它是'static'和「只讀」 – Geros 2013-02-12 02:28:25

+0

strings.xml被編譯器用來創建.apk文件。 Android設備對此文件不瞭解。 – 2013-02-12 03:22:32

回答

0

我不知道你在哪裏得到這個想法,但它是靜態的,因此不能在運行時改變。

您可以但是做的工作在這裏

首先創建一個類,並添加一個公共靜態字符串爲

public class StringGen { 

public static String username 
    public static String password 
} 

設置你的數據也許登錄

StringGen.username = *yourString* 

只需在您的應用上調用它(活動等)

String myUsername = StringGen.username 

乾杯

相關問題