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
}
});
} }
谢谢您的时间
'Strings.xml'不能在运行时由应用程序改变。保存到SharedPreferences /数据库并从那里读取。我不知道你是如何得到'SharedPreferences'保存到'Strings.xml'的。 – 2013-02-12 02:19:37
这是不可能的,它是'static'和“只读” – Geros 2013-02-12 02:28:25
strings.xml被编译器用来创建.apk文件。 Android设备对此文件不了解。 – 2013-02-12 03:22:32