【问题标题】:Android : How to Store the Value for a Variable in Strings.xml through codingAndroid:如何通过编码在 Strings.xml 中存储变量的值
【发布时间】:2013-02-12 02:18:05
【问题描述】:

需求:我需要将User Id和Password的值作为字符串值存储在String.xml中,确实是用户自己获取的。

问题:据我所知,解决方案是使用共享首选项。虽然我使用 Shared Preferences ,但值不会存储在 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/Database 并从那里读取。我不知道你是怎么想到SharedPreferences 保存到Strings.xml 的。
  • 不可能是static和“只读”
  • 编译器使用 strings.xml 来创建您的 .apk 文件。 Android 设备对此文件一无所知。
  • 您还可以将数据保存在文件、静态变量或数据库中...

标签: java android xml


【解决方案1】:

我不知道你从哪里得到这个想法,但它是静态的,因此无法在运行时更改。

但是你可以在这里做一个工作

首先创建一个类并为其添加一个公共静态字符串

例如

public class StringGen {

public static String username
    public static String password
}

设置您的数据或登录

StringGen.username = *yourString*

只需在您的应用程序(活动等)上调用它

String myUsername = StringGen.username

干杯

【讨论】:

    猜你喜欢
    • 2014-10-14
    • 1970-01-01
    • 2021-12-29
    • 1970-01-01
    • 2017-02-03
    • 2019-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多