java - Android : How to Store the Value for a Variable in Strings. xml通过编码

标签 java android xml

需求:我需要将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


     }

    });
} }

谢谢你的时间

最佳答案

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

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

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

例如

public class StringGen {

public static String username
    public static String password
}

设置您的数据或登录

StringGen.username = *yourString*

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

String myUsername = StringGen.username

干杯

关于java - Android : How to Store the Value for a Variable in Strings. xml通过编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14824424/

相关文章:

java - 在当前帧之前存储视频帧?

android - 以编程方式显示 NavigationView MenuItems 全息指示器

c# - XSD2Code 类需要包含元素集合的重名元素

android - UnsatisfiedLinkError - Android 版 Tesseract

c# - 列表的 XML 序列化变得太大

java - 谷歌地图显示空白屏幕,左下角有 'Google' 标志

java - 在 Java 中声明私有(private)方法 "final"有优势吗?

java - 构建 Stanford CoreNLP 时出错

java - Spring WebFlux WebClient : delay execution

Android Facebook SDK (4.31.0) - CustomTabLoginMethodHandler 中的 ActivityNotFoundException