2

您好,我是 android 新手,实际上我正在开发一个应用程序,用户将在该应用程序上单击一个按钮,并且该按钮应记录单击事件 - 每次单击该按钮时计数器应递增。该按钮将显示在一个活动中,一旦用户单击该按钮,将显示另一个活动,从而显示结果。

实际上,我在将 sharedPreferences 分配给按钮然后将其显示到下一个活动中时遇到问题,因此有点击次数。

我正在使用的代码如下:

MainActivity.java

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */

    /** Declare the variables being used */
    public static final String GAME_PREFERENCES = "GamePrefs";

     public static final String GAME_PREFERENCES_SCORE = "Score"; // Integer
    int counter;
    Button add;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        counter = 0;
        add = (Button) findViewById (R.id.bAdd);
        add.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                counter++;

                Intent openClickActivity2 = new Intent("com.android.jay.Results");
                startActivity(openClickActivity2);

            }
        });
       }
}

结果.java

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.TextView;
public class Results extends MainActivity{

    public void onCreate(Bundle savedInstanceState) {

        SharedPreferences mGameSettings;
        super.onCreate(savedInstanceState);
        mGameSettings = getSharedPreferences(GAME_PREFERENCES, Context.MODE_PRIVATE);
        setContentView(R.layout.results);

        final TextView DisplayResults =
                (TextView) findViewById(R.id.bAdd);
                if (mGameSettings.contains(GAME_PREFERENCES_SCORE)) {
                DisplayResults.setText(mGameSettings.getString(GAME_PREFERENCES_SCORE, “counter”));
                }
        }

}

任何指导我的帮助将不胜感激。谢谢

4

3 回答 3

1

Simply make a Preferences class

public class Preferences {

String MASTER_NAME = "mysticmatrix_master";
SharedPreferences mysticMatrixPref;

Preferences(Context context) {
    mysticMatrixPref = context.getSharedPreferences(MASTER_NAME, 0);
}

public void setAddCount(int count) {
    SharedPreferences.Editor prefEditor = mysticMatrixPref.edit();
    prefEditor.putInt("count", count);
    prefEditor.commit();

}

public int getAddCount() {
    return mysticMatrixPref.getInt("count", 0);
}
}

and in your MainActivity.java put this code

public class MainActivity extends Activity implements OnClickListener {

ImageButton add;
Preferences cpObj;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    preferences = new Preferences(getApplicationContext());

    /*
     * getting the count variable and adding 1 in that to check the condition of showing rate activity and adds
     */
     add = (Button) findViewById (R.id.bAdd);
    add.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
         cpObj = new Preferences(getApplicationContext());
         cpObj.setAddCount(cpObj.getAddCount() + 1);

        }
    });
    }
    }

And in your result activity just get the count's value

import android.content.Context;
public class Results extends MainActivity{
Preferences cpObj;

public void onCreate(Bundle savedInstanceState) {

     preferences = new Preferences(getApplicationContext());
    setContentView(R.layout.results);

    final TextView DisplayResults =
            (TextView) findViewById(R.id.bAdd);
            DisplayResults.setText(cpObj.getAddCount());
            }
    } }

By this you will get the default value of result as '0' and you can set that in your Preferences class

于 2012-10-13T10:35:49.580 回答
1

您必须在 MainActivity 中设置 GAME_PREFERENCES_SCORE。在 counter++ 之后做这样的事情:

getSharedPreferences(GAME_PREFERENCES, Context.MODE_PRIVATE).edit().setInt(GAME_PREFERENCES_SCORE, counter)。犯罪();

于 2012-10-13T10:23:23.827 回答
0

使用这样的方法:

    public static void SavePreference(Context context, String key, Integer value) {
    SharedPreferences.Editor editor = PreferenceManager
            .getSharedPreferences(GAME_PREFERENCES, Context.MODE_PRIVATE)
            .edit();
    editor.putInt(key, value);
    editor.commit();
}

在你的 onclick 之后 counter++ 添加这个:

SavePereference(context, "GAME_PREFERENCES_SCORE", counter);
于 2012-10-13T10:16:42.330 回答