0

我是android的新手。我正在使用galary。我想知道当我单击下一个图库项目时如何保存以前的按钮状态。我想在所有图像上关闭默认按钮状态以及何时选择按钮状态当我点击下一张图片时,它应该是安全的。当我回到上一张图片时,它应该显示保存按钮状态。我试图比较图像ID和按钮选择的图像ID。但这不是很好的工作。怎么办这个?非常感谢功能帮助。

 imageView = (ImageView) findViewById(R.id.ImageSportView);
    imageView.setImageResource(imgID[0]);
    sportButton = (Button) findViewById(R.id.SportButton1);

    gallery = (Gallery) findViewById(R.id.SportGallery);
    // creating AddImgadapter
    gallery.setAdapter(new AddImgAdapter(this));

        @Override
        public void onItemClick(AdapterView<?> perent, View view,

int position, long id) {

            // getting id position
            setSelected(position);
            // deselect button on item click
            onImageClick(view, position);
            Log.d(MY_LOG, "img click");
                        Log.d(MY_LOG, "img Id" + position);

        }

    });

}

// deselect button on image click
int itemPosition = 1;

public void onImageClick(View button, int position) {
    itemPosition = position;
    if (selectedPosition != position) {

        sportButton.setSelected(false);

        Log.d(MY_LOG, "selected postion " + selectedPosition + " = "
                + "item position" + position);

    } else {
        if (selectedPosition == position) {
            sportButton.setSelected(true);
        }
        Log.d(MY_LOG, "selected postion " + selectedPosition + " = "
                + "item position " + position);
    }

}

// getting Id current item
int selectedPosition = -1;

private void setSelected(int position) {

    selectedPosition = position;
    imageView.setImageResource(imgID[selectedPosition]);

}

public void onClickButton(View button) {

    if (button.isSelected()) {

        button.setSelected(false);

        Log.d(MY_LOG, "click off");
    } else {
        // on button click on we select picture id
        button.setSelected(true);
        if (selectedPosition != -1) {
            Log.d(MY_LOG, "selected position " + selectedPosition);
            //selectedImage(selectedPosition);
            //Intent intent = new Intent(Sport.this, Favorites.class);
        }
        Log.d(MY_LOG, "click on");

    }

}
4

1 回答 1

0

保存前一个按钮状态的一种方法是使用 Android 中的共享首选项。共享首选项允许存储数据的键值对,以便以后轻松检索。Android 中有一种数据访问机制。其他是 SqlLite 数据库和文件。

关于共享偏好的 Android 文档

关于共享偏好的视频

现在再次回到您的问题。我曾经不得不保存 checkbutton 的状态。然后稍后再次访问它(这似乎也与您想要做的类似)

    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-05-15T08:34:45.510 回答