【问题标题】:Static Value Android Studio静态值 Android Studio
【发布时间】:2016-02-12 04:01:36
【问题描述】:

我有两个活动。还有一个称为计数器的静态整数。

所以,如果我在活动“A”中按下一个按钮,那么counter = counter + 1

这是活动 a 的代码:

public static int counter = 0;
cmdOk.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        counter = counter + 1;
        if (counter == 5)
        {
             tagihan.txtShip1.setTextColor(Color.parseColor("#000000"));
             tagihan.txtNilai1.setTextColor(Color.parseColor("#000000"));
             tagihan.txtSupir1.setTextColor(Color.parseColor("#000000"));
        }
}

这是来自活动 b :

cmdSuccess.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        a.counter = a.counter + 1;
        if (a.counter == 5)
        {
             tagihan.txtShip1.setTextColor(Color.parseColor("#000000"));
             tagihan.txtNilai1.setTextColor(Color.parseColor("#000000"));
             tagihan.txtSupir1.setTextColor(Color.parseColor("#000000"));
        }
}

我的问题是,当我尝试从活动中按下按钮 3 次时,它可以正常工作。所以现在的值为 3。

但是当我尝试按下活动 b 中的按钮时,该值将重新启动为 0。实际上我并没有破坏活动 a。

所以我想要的是,即使我从活动 a 或 b 按下,值也会持续变化。

有什么想法吗?

已编辑:

我已经编辑了代码。 Tagihan 活动是我试图完成的。所以当计数器为 5 时,tagihan 活动正在发生变化。

【问题讨论】:

  • 在活动之间共享静态动态变量不是首选,因为您几乎无法控制活动生命周期。您要解决的问题是什么?
  • 你必须展示这两个活动的完整来源。
  • 如何将活动 a 的实例传递给 b 。你能展示一下吗?
  • 我已经编辑了我的问题。请检查一下
  • 您可以将其存储在 sharedprefrence 中,也可以将其存储在应用程序类中

标签: android android-activity static-members


【解决方案1】:

不要使用静态数据,这是一种不好的方法,也不是常见的 OOP 开发方式,而是尝试在活动之间传递数据...

第一幕

Intent intent = new Intent(activity2.this, activity1.class);
intent.putExtra("message", message);
startActivity(intent);

第二幕:

Bundle bundle = getIntent().getExtras();
String message = bundle.getString("message");

Android 开发网站对此做了介绍: http://developer.android.com/training/basics/firstapp/starting-activity.html

【讨论】:

    【解决方案2】:

    编辑后,我可以看到您需要一个可以对所有活动进行读/写的“全局变量”:

    解决方案: 所有活动都嵌入在应用程序中,因此如果您在应用程序中拥有字段/成员,则可以使用标准 setter/getter

    访问它们

    你需要:

    定义一个应用程序

    public class MyApplication extends Application {
    
        private int counterVariable;
    
        public int counterVariable() {
            return this.counterVariable;
        }
    
        public void setCounterVariable(int someVariable) {
            this.counterVariable = someVariable;
        }
    }
    

    将应用添加到清单中:

    <application 
      android:name="MyApplication" 
      android:icon="@drawable/icon" 
      android:label="@string/app_name">
    

    然后在您的活动中获取并设置变量,如下所示:

    // cast to Application and call the setter
    ((MyApplication) this.getApplication()).counterVariable(1);
    
    // cast to Application and call the getter
    int counter = ((MyApplication) this.getApplication()).getCounterVariable ();
    

    【讨论】:

      【解决方案3】:

      请使用以下代码:

      // 避免静态值持有的广义形式:

      public class SPDataHandler {
      
      
       private Context mContext;
       private SharedPreferences mPreference;
       public SPDataHandler(Context context) {
              this.mContext = context;
              this.mPreference = mContext.getSharedPreferences("SAMPLE_SP", Context.MODE_PRIVATE);
         }
          /**
           * COMMON SETTER FOR INTEGER DATA
           */
          private void setIntegerData(String key, int value) {
              SharedPreferences.Editor editor = mPreference.edit();
              editor.putInt(key, value);
              editor.commit();
          }
          /**
           * COMMON GETTER FOR INTEGER SP DATA
           */
          private int getIntegerSpData(String key, int defaultValue) {
              return this.mPreference.getInt(key, defaultValue);
          }
      
      
          // Your Getter and Setter
      
           public int getmCount() {
              return this.getIntegerSpData("Count", 1);
          }
      
          public void setmCount(int cont) {
              this.setIntegerData("Count", cont);
          }
      }
      
      // Your Activity A
      
      SPDataHandler handler = new  SPDataHandler(this);
      int count = handler.getmCount();
      cmdOk.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
              count = count + 1;
              handler.setmCount(count); // Change the logic based on your requirement
              if (count == 5)
              {
                   tagihan.txtShip1.setTextColor(Color.parseColor("#000000"));
                   tagihan.txtNilai1.setTextColor(Color.parseColor("#000000"));
                   tagihan.txtSupir1.setTextColor(Color.parseColor("#000000"));
              }
      }
      
      
      // Your Activity B
      SPDataHandler handler = new  SPDataHandler(this);
      int count = handler.getmCount();
      cmdSuccess.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
             count = count + 1;
              handler.setmCount(count); // Change the logic based on your requirement
              if (count  == 5)
              {
                   tagihan.txtShip1.setTextColor(Color.parseColor("#000000"));
                   tagihan.txtNilai1.setTextColor(Color.parseColor("#000000"));
                   tagihan.txtSupir1.setTextColor(Color.parseColor("#000000"));
              }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-05-16
        • 1970-01-01
        • 2016-04-14
        • 2015-09-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多