如何在android中为当前颜色设置颜色?

时间:2016-05-15 10:35:03

标签: android

我的日历中有这个代码。我想为当天的颜色着色,但它在所有月份都是当天的颜色。例如今天15/05我的代码颜色15 / 05,15 / 06,15 / 07,15 / 08 ...... 我怎么能这样做我需要你的帮助

修改

    public View getView(int position, View convertView, ViewGroup parent) {
                View row = convertView;
                if (row == null) {
                    LayoutInflater inflater = (LayoutInflater) _context
                            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    row = inflater.inflate(R.layout.screen_gridcell, parent, false);
                }
                // Get a reference to the Day gridcell
                gridcell = (Button) row.findViewById(R.id.calendar_day_gridcell);
                gridcell.setOnClickListener(this);
                // ACCOUNT FOR SPACING
                Log.d(tag, "Current Day: " + getCurrentDayOfMonth());
                String[] day_color = list.get(position).split("-");
                String theday = day_color[0];
                String themonth = day_color[2];
                String theyear = day_color[3];
                if ((!eventsPerMonthMap.isEmpty()) && (eventsPerMonthMap != null)) {
                    if (eventsPerMonthMap.containsKey(theday)) {
                        num_events_per_day = (TextView) row
                                .findViewById(R.id.num_events_per_day);
                        Integer numEvents = (Integer) eventsPerMonthMap.get(theday);
                        num_events_per_day.setText(numEvents.toString());
                    }
                }
                // Set the Day GridCell
                gridcell.setText(theday);
                gridcell.setTag(theday + "-" + themonth + "-" + theyear);
                Log.d(tag, "Setting GridCell " + theday + "-" + themonth + "-"
                        + theyear);
                if (day_color[1].equals("GREY")) {
                    gridcell.setTextColor(getResources()
                            .getColor(R.color.lightgray));
                }
                if (day_color[1].equals("WHITE")) {
                    gridcell.setTextColor(getResources().getColor(
                            R.color.lightgray02));
                }
                if (day_color[1].equals("BLUE")) {
                    gridcell.setTextColor(getResources().getColor(R.color.orrange));
                }
getJSON();
                return row;
            }
private void getJSON() {
            class GetJSON extends AsyncTask<Void, Void, String> {
                ProgressDialog loading;
                @Override
                protected void onPreExecute() {
                    super.onPreExecute();
                    loading = ProgressDialog.show(getActivity(), "Téléchargement", "Veuillez patientez...", false, false);
                }
                @Override
                protected void onPostExecute(String s) {
                    super.onPostExecute(s);
                    loading.dismiss();
                    JSON_STRING = s;
                    JSONObject jsonObject = null;
                   // ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String, String>>();
                    try {
                        jsonObject = new JSONObject(JSON_STRING);
                        JSONArray result = jsonObject.getJSONArray(TAG_JSON_ARRAY);
                        num=result.length();
                        if (day_color[1].equals("BLUE") && num>0){
                            gridcell.setTextColor(getResources().getColor(R.color.orrange));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                @Override
                protected String doInBackground(Void... params) {
                    RequestHandler rh = new RequestHandler();
                    String s = rh.sendGetRequest(URL);
                    return s;
                }
            }
            GetJSON gj = new GetJSON();
            gj.execute();
        }
            @Override
            public void onClick(View view) {
                String date_month_year = (String) view.getTag();
                //selectedDayMonthYearButton.setText("Selected: " + date_month_year);
                Log.e("Selected date", date_month_year);
                try {
                    Date parsedDate = dateFormatter.parse(date_month_year);
                    Log.d(tag, "Parsed Date: " + parsedDate.toString());

                } catch (ParseException e) {
                    e.printStackTrace();
                }
                final String log = et_login.getText().toString().trim();
                Intent intent = new Intent(getActivity(), RendezVousListe.class);
                intent.putExtra("dateSel", date_month_year);
                intent.putExtra("log", log);
                startActivity(intent);
            }
            public int getCurrentDayOfMonth() {
                return currentDayOfMonth;
            }
            private void setCurrentDayOfMonth(int currentDayOfMonth) {
                this.currentDayOfMonth = currentDayOfMonth;
            }
            public void setCurrentWeekDay(int currentWeekDay) {
                this.currentWeekDay = currentWeekDay;
            }            public int getCurrentWeekDay() {
                return currentWeekDay;
            }

php code

<?php 
        $log1= $_POST['log'];
        $dat= $_POST['dateR'];
        require_once('dbConnect.php');
        $sql = "SELECT * FROM rendezvous WHERE LoginD = '$log1' and DateR='$dat'";

    //getting result 
    $r = mysqli_query($con,$sql);

    //creating a blank array 
    $result = array();
    $res = mysqli_num_rows($r):
    //looping through all the records fetched
    while($row = mysqli_fetch_array($r)){

        //Pushing name and id in the blank array created 
        array_push($result,array(
            "id"=>$row['id'],
            "LoginM"=>$row['LoginM'],
            "HeureR"=>$row['HeureR']


        ));
    }

    //Displaying the array in json format 
    echo json_encode(array('result'=>$result));

    mysqli_close($con);

1 个答案:

答案 0 :(得分:0)

此代码(在所有其他问题中)的问题在于它不区分月/年。如果您希望此代码仅在当前月份中选择当天,则您只需在printMonth方法中添加月/年检查。

所以更改printMonth方法的这部分代码:

    // Current Month Days
    for (int i = 1; i <= daysInMonth; i++) {
        Log.d(currentMonthName, String.valueOf(i) + " "
                    + getMonthAsString(currentMonth) + " " + yy);
        if (i == getCurrentDayOfMonth()) {
            list.add(String.valueOf(i) + "-BLUE" + "-"
                        + getMonthAsString(currentMonth) + "-" + yy);
        } else {
            list.add(String.valueOf(i) + "-WHITE" + "-"
                    + getMonthAsString(currentMonth) + "-" + yy);
        }
    }

这样的事情:

    Calendar calendar = Calendar.getInstance();

    // Current Month Days
    for (int i = 1; i <= daysInMonth; i++) {
        Log.d(currentMonthName, String.valueOf(i) + " "
                + getMonthAsString(currentMonth) + " " + yy);
        if (i == getCurrentDayOfMonth() && calendar.get(Calendar.MONTH) == currentMonth && calendar.get(Calendar.YEAR) == yy) {
            list.add(String.valueOf(i) + "-BLUE" + "-"
                        + getMonthAsString(currentMonth) + "-" + yy);
        } else {
            list.add(String.valueOf(i) + "-WHITE" + "-"
                        + getMonthAsString(currentMonth) + "-" + yy);
        }
    }