【问题标题】:Shared preference共享偏好
【发布时间】:2010-09-08 07:04:16
【问题描述】:

我开发了一个应用程序,我想将 URI 从 Class1 editText 发送到另一个包含 editText 的类。
谁能告诉我该怎么做?

【问题讨论】:

  • 这与共享偏好有什么关系?

标签: android android-activity android-intent bundle


【解决方案1】:

SharedPreferences 是错误的做法。使用每个 Intent 可以拥有的 Bundle 功能:http://developer.android.com/reference/android/content/Intent.html

在第二个活动中,您可以致电getExtra(),然后就可以了...

【讨论】:

    【解决方案2】:

    假设你想使用 SharedPreferences 来传输 URI,你可以试试这个:

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    prefs.edit().putString("my-uri", "http://google.com/").commit();
    

    然后检索 URI:

    prefs.getString("my-uri", "default URI");
    

    如果您的两个类是活动,并且其中一个启动另一个,您可能应该将 URI 作为额外的意图传递。

    另外,阅读FAQ 并接受一些答案!

    【讨论】:

      【解决方案3】:

      您也可以使用 System.setProperty/get Property。

      【讨论】:

        【解决方案4】:

        你不喜欢在意图中添加 putExtra

        喜欢这个

        Intent i = new Intent(getApplicationContext(), Audit_FSD_Tab.class);
                    i.putExtra("UsrID", UsrID);
                    i.putExtra("Store", Store);
                    i.putExtra("location", location);
                    startActivityForResult(i, 0);
        

        现在在其他活动中访问这些额外的

             Bundle UsrVal = null;
                UsrVal = this.getIntent().getExtras();
            UsrID = UsrVal.getString("UserId");
            Store = UsrVal.getString("Store");
            location = UsrVal.getString("location");
        

        【讨论】:

          【解决方案5】:

          尝试将 Uri 存储在第一个活动的共享首选项内的编辑文本中,然后在第二个活动的创建方法中从共享首选项中检索 Uri 值并将其显示在编辑 text.simple...

          【讨论】:

            【解决方案6】:

            例如,可以通过使用共享首选项来实现

             SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE); 
             Editor editor = pref.edit();
             data=pref.getString("key_name5", null);
             editText.setText(data);
            

            你可以关注tutorial here

            http://firstcode.info/android-sharedpreferences-basics/

            【讨论】:

              猜你喜欢
              • 2014-07-29
              • 2019-03-20
              • 1970-01-01
              • 1970-01-01
              • 2017-05-17
              • 2013-03-15
              • 2013-05-10
              • 2019-06-20
              相关资源
              最近更新 更多