如何检查共享首选项的值以检查条件

时间:2015-10-19 08:19:36

标签: java android

这里我有三个按钮是否可能三个按钮当按钮点击时我改变了颜色并将共享偏好中的点击按钮的值存储在按住按钮时保持颜色

@覆盖         public View getView(final int position,View convertView,ViewGroup parent){

        if (convertView == null)
            convertView = mInflater.inflate(R.layout.invitation, null);
        eventNameTxtV = (TextView) convertView.findViewById(R.id.invitation_title);
        eventPlaceTxtV = (TextView) convertView.findViewById(R.id.invitation_place);
        eventNameTxtV.setText(eventMOs.get(position).getText());
        eventPlaceTxtV.setText(eventMOs.get(position).getPlace());

        convertView.setTag(position);
        View v = convertView.findViewById(R.id.invitation_single);

        final Button yesBtn = (Button) convertView.findViewById(R.id.yesbutton);
        final Button noBtn = (Button) convertView.findViewById(R.id.nobutton);
        final Button maybeBtn = (Button) convertView.findViewById(R.id.buttonmaybe);


        final LinearLayout eventLayout = (LinearLayout) convertView.findViewById(R.id.invitation_single);


        final LinearLayout responseLayout = (LinearLayout) convertView.findViewById(R.id.hidden);

        //Based on the user click, response will be stored



        yesBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                // highlight the button when clicked
                yesBtn.setBackgroundColor(Color.YELLOW);
                noBtn.setBackgroundColor(Color.BLUE);
                maybeBtn.setBackgroundColor(Color.BLUE);
                responseLayout.setVisibility(View.GONE);
                //If user clicks yes button in invitation response layout,response would be stored as 1 for event user
                final int response = 1;
                final long eventId = eventMOs.get(position).getEventId();
                userMO.setIsAttending(response);
                //create shared preferences here

                prefs =getActivity().getSharedPreferences("mypref", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor =prefs.edit();
                editor.putString("buttonClicked","true");
                editor.commit();

                /*SharedPreferences sharedpreferences = getActivity().getSharedPreferences("PREFERENCE", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedpreferences.edit();
                editor.putInt("clicked_btn", 1);
                editor.commit();*/
                new AsyncTask<Void, Void, String>() {
                    protected String doInBackground(Void... arg0) {
                        return userDelegate.updateEventUserRelationShipMapping(userMO, eventId);

                    }

                }.execute(null, null, null);

            }

        });

        noBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                yesBtn.setBackgroundColor(Color.BLUE);
                noBtn.setBackgroundColor(Color.YELLOW);
                maybeBtn.setBackgroundColor(Color.BLUE);
                responseLayout.setVisibility(View.GONE);
                //If user clicks no button in invitation response layout,response would be stored as 0 for event user
                final int response = 0;
                final long eventId = eventMOs.get(position).getEventId();
                userMO.setIsAttending(response);

                SharedPreferences sharedpreferences = getActivity().getSharedPreferences("PREFERENCE", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedpreferences.edit();
                editor.putInt("clicked_btn", 0);
                editor.commit();

                new AsyncTask<Void, Void, String>() {
                    protected String doInBackground(Void... arg0) {
                        return userDelegate.updateEventUserRelationShipMapping(userMO, eventId);

                    }

                }.execute(null, null, null);
            }
        });
        maybeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                yesBtn.setBackgroundColor(Color.BLUE);
                noBtn.setBackgroundColor(Color.BLUE);
                maybeBtn.setBackgroundColor(Color.YELLOW);
                responseLayout.setVisibility(View.GONE);
                //If user clicks maybe button in invitation response layout,response would be stored as  for event user
                final int response = 2;
                userMO.setIsAttending(response);
                final long eventId = eventMOs.get(position).getEventId();

                SharedPreferences sharedpreferences = getActivity().getSharedPreferences("PREFERENCE", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedpreferences.edit();
                editor.putInt("clicked_btn",2);
                editor.commit();

                new AsyncTask<Void, Void, String>() {
                    protected String doInBackground(Void... arg0) {
                        return userDelegate.updateEventUserRelationShipMapping(userMO, eventId);

                    }

                }.execute(null, null, null);
            }
        });

如果我选择了任何一个按钮,那么每当我返回应用程序时,我必须按住按钮的颜色变化。所以如何使用和检索共享首选项值

这是我单击事件时显示是否可能按钮的代码

eventLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                                      switch (v.getId()) {
                        case R.id.invitation_single:
                            responseLayout.setVisibility(View.VISIBLE);
                            break;

                    }
                }


           });

2 个答案:

答案 0 :(得分:0)

SharedPreferences可让您存储不同类型的数据,例如booleanint

要完成任务,您可以将每个按钮的颜色存储在SharedPreferences;类似的东西:

 editor.putInt("button_one", R.color.buttone_one_selected);

请记住,当您检索该颜色时,您必须使用以下方法解决它:

int buttonOneColor = sharedPrefs.getInt("button_one", default_value); 
int colorBackground = getResources().getColor(buttonOneColor);

答案 1 :(得分:0)

您可以使用此更改按钮的颜色......

on onCreate:

SharedPreferences prefs = getSharedPreferences("myPrefs",
                Context.MODE_PRIVATE);

现在检查存储在首选项中的布尔值:

boolean b1_pressed = prefs.getBoolean("button1_pressed",false);

if(b1_pressed){

//code to change color of button to pressed state

}

else{

//code to change color of button to normal state

}

现在在按钮的onClick方法中:

if(b1_pressed){

SharedPreferences.editor editor = prefs.edit();

editor.putBoolean("button1_pressed",false);

editor.commit();

}
else{

SharedPreferences.editor editor = prefs.edit();

editor.putBoolean("button1_pressed",true);

editor.commit();

}