0

私は Android アプリを作成しています。ユーザーがボタンをクリックする回数を設定で保存し、その設定を別のクラスで取得したいと考えています。私はこの時点で完全な初心者であり、助けていただければ幸いです。

4

4 に答える 4

1

以下のようなものを試してください:

Activity1.java

SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(Activity1.this);
SharedPreferences.Editor editor = app_preferences.edit();
int i=0;

    yourbutton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                                    i++;
                    editor.putInt("counter", i);
                    editor.commit();
                }
            });

Activity2.java

SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
String counter = app_preferences.getInt("counter", 0);
于 2013-10-16T04:20:59.527 に答える
1

こうすれば..

**Activity1.java**
------------------

SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
int myIntValue = sp.getInt("your_int_key",0);


        yourbutton.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {

                    editor.putInt("your_int_key",++myIntValue);
                                editor.commit();
                    }
                });

**Activity2.java**
-----------------

  SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
 int myIntValue = sp.getInt("your_int_key", 0);
于 2013-10-16T04:56:22.397 に答える
0
// declare thsi class variable in class from where u will put the string u wanna store in shared pref 

//class variables

    SharedPreferences pref;
    SharedPreferences.Editor editor;
-------------------

//in oncrete method
// declare this in oncreate method 

        pref = getSharedPreferences("testapp", MODE_PRIVATE);
        editor = pref.edit();

// the varibale u wanna put use the below statements 
// for string use putString
// for boolean as u need use putBoolean 
// have a look at the various option it offers..


editor.putString("selected", "nil");
editor.commit();


// here is the statement use this statement in class where u wanna retireve ur strings
// use getBoolean for Boolean variables 

pref.getString("selected", "nil")

// here in sceond parameter in above statement is : if the value u r requesting for that is specified in first parameter is not present then it will return the //value which is your second parameter..
于 2013-10-16T04:13:03.667 に答える
0

前のボタンの状態を保存する 1 つの方法は、 Android の共有設定を利用することです。共有プリファレンスを使用すると、データのキーと値のペアを保存して、後で簡単に取得できます。Android には dataaccess メカニズムの 1 つがあります。その他は SqlLite Database & Files です。

共有設定に関する Android ドキュメント

共有設定に関するビデオ

ここで再び問題に戻ります。私はかつてcheckedbuttonの状態を保存しなければなりませんでした。その後、もう一度アクセスします(これは、やりたいことと似ているようです)

    Part 1 Accessing Share preference Values : 

        public static final String PREFS_FILE = "MyPreferences";
        public static final String PREFS_NAME = "USER_NAME";
        public static final String PREFS_CHOICE = "USER_CHOICE";

        SharedPreferences sp;

        public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            chkChoice = (CheckBox)findViewById(R.id.chkChoice);
            btnMain = (Button)findViewById(R.id.btnMain);
            btnMain.setOnClickListener(this);

            // Here i access the shared preference file .
            sp = this.getSharedPreferences(PREFS_FILE, MODE_PRIVATE);
            // If i have a preference for my checkbox saved then load it else FALSE(unchecked) 
            chkChoice.setChecked(sp.getBoolean(PREFS_CHOICE, false));
        }


   Part 2 Setting Share preference from your activity :

    sp = this.getSharedPreferences(PREFS_FILE, MODE_PRIVATE);
    SharedPreferences.Editor editor = sp.edit();

    editor.putString(PREFS_NAME, txtName.getText().toString());
    editor.putBoolean(PREFS_CHOICE, chkChoice.isChecked());

    editor.commit();

    // Close the activity to show that the data is still saved
    finish();

上記はチェックボックス用です。保存したいボタン情報の種類に合わせて調整する必要があります。これで始められることを願っています。

于 2013-10-16T04:19:19.507 に答える