有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

这里我有三个按钮是否可能对于三个按钮,我在按钮点击时更改了颜色,并将点击按钮的值存储在共享首选项中,以便在我返回按钮时保持颜色

@覆盖 公共视图getView(最终整数位置、视图转换视图、视图组父视图){

        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) 个答案

  1. # 1 楼答案

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

    在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();
    
    }
    
  2. # 2 楼答案

    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);