首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在何处添加以下代码中的onclicklistener函数以选择日历中的网格视图

在何处添加以下代码中的onclicklistener函数以选择日历中的网格视图
EN

Stack Overflow用户
提问于 2020-10-15 14:08:18
回答 2查看 35关注 0票数 0

我正在尝试为日历中的特定网格编写onClickListener函数,以便可以将事件添加到特定日期,因此请帮助我在何处编写该函数以选择日历中的日期网格。

代码语言:javascript
复制
package com.example.myfitness;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class CalendarAdapter extends BaseAdapter {
    private List<Date> dateArray = new ArrayList();
    private Context mContext;
    private DateManager mDateManager;
    private LayoutInflater mLayoutInflater;

    //カスタムセルを拡張したらここでWigetを定義
    private static class ViewHolder {
        public TextView dateText;
    }

    public CalendarAdapter(Context context){
        mContext = context;
        mLayoutInflater = LayoutInflater.from(mContext);
        mDateManager = new DateManager();
        dateArray = mDateManager.getDays();
    }

    @Override
    public int getCount() {
        return dateArray.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.calendar_cell, null);
            holder = new ViewHolder();
            holder.dateText = convertView.findViewById(R.id.dateText);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }

        //セルのサイズを指定
        float dp = mContext.getResources().getDisplayMetrics().density;
        AbsListView.LayoutParams params = new AbsListView.LayoutParams(parent.getWidth()/7 - (int)dp, 
        (parent.getHeight() - (int)dp * mDateManager.getWeeks() ) / mDateManager.getWeeks());
        convertView.setLayoutParams(params);

        //日付のみ表示させる
        SimpleDateFormat dateFormat = new SimpleDateFormat("d", Locale.US);
        holder.dateText.setText(dateFormat.format(dateArray.get(position)));

        //当月以外のセルをグレーアウト
        if (mDateManager.isCurrentMonth(dateArray.get(position))){
            convertView.setBackgroundColor(Color.WHITE);
        }else {
            convertView.setBackgroundColor(Color.LTGRAY);
        }

        //日曜日を赤、土曜日を青に color used for weekends
        int colorId;
        switch (mDateManager.getDayOfWeek(dateArray.get(position))){
            case 1:
                colorId = Color.RED;
                break;
            case 7:
                colorId = Color.BLUE;
                break;

            default:
                colorId = Color.BLACK;
                break;
        }
        holder.dateText.setTextColor(colorId);

        return convertView;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    //表示月を取得 display the current month
    public String getTitle(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy.MM", Locale.US);
        return format.format(mDateManager.mCalendar.getTime());
    }

    //翌月表示 display the next month
    public void nextMonth(){
        mDateManager.nextMonth();
        dateArray = mDateManager.getDays();
        this.notifyDataSetChanged();
    }

    //前月表示 display the previous month
    public void prevMonth(){
        mDateManager.prevMonth();
        dateArray = mDateManager.getDays();
        this.notifyDataSetChanged();
    }
}
EN

回答 2

Stack Overflow用户

发布于 2020-10-15 14:38:18

请尝试一下,注意getView()方法中的最后代码:

代码语言:javascript
复制
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class CalendarAdapter extends BaseAdapter {
    private List<Date> dateArray = new ArrayList();
    private Context mContext;
    private DateManager mDateManager;
    private LayoutInflater mLayoutInflater;

    //カスタムセルを拡張したらここでWigetを定義
    private static class ViewHolder {
        public TextView dateText;
    }

    public CalendarAdapter(Context context){
        mContext = context;
        mLayoutInflater = LayoutInflater.from(mContext);
        mDateManager = new DateManager();
        dateArray = mDateManager.getDays();
    }

    @Override
    public int getCount() {
        return dateArray.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.calendar_cell, null);
            holder = new ViewHolder();
            holder.dateText = convertView.findViewById(R.id.dateText);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }

        //セルのサイズを指定
        float dp = mContext.getResources().getDisplayMetrics().density;
        AbsListView.LayoutParams params = new AbsListView.LayoutParams(parent.getWidth()/7 - (int)dp,
                (parent.getHeight() - (int)dp * mDateManager.getWeeks() ) / mDateManager.getWeeks());
        convertView.setLayoutParams(params);

        //日付のみ表示させる
        SimpleDateFormat dateFormat = new SimpleDateFormat("d", Locale.US);
        holder.dateText.setText(dateFormat.format(dateArray.get(position)));

        //当月以外のセルをグレーアウト
        if (mDateManager.isCurrentMonth(dateArray.get(position))){
            convertView.setBackgroundColor(Color.WHITE);
        }else {
            convertView.setBackgroundColor(Color.LTGRAY);
        }

        //日曜日を赤、土曜日を青に color used for weekends
        int colorId;
        switch (mDateManager.getDayOfWeek(dateArray.get(position))){
            case 1:
                colorId = Color.RED;
                break;
            case 7:
                colorId = Color.BLUE;
                break;

            default:
                colorId = Color.BLACK;
                break;
        }
        holder.dateText.setTextColor(colorId);
        holder.dateText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Your events for the particular date here!
            }
        });

        return convertView;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Object getItem(int position) {
        return dateArray.get(position);
    }

    //表示月を取得 display the current month
    public String getTitle(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy.MM", Locale.US);
        return format.format(mDateManager.mCalendar.getTime());
    }

    //翌月表示 display the next month
    public void nextMonth(){
        mDateManager.nextMonth();
        dateArray = mDateManager.getDays();
        this.notifyDataSetChanged();
    }

    //前月表示 display the previous month
    public void prevMonth(){
        mDateManager.prevMonth();
        dateArray = mDateManager.getDays();
        this.notifyDataSetChanged();
    }
}
票数 0
EN

Stack Overflow用户

发布于 2020-10-15 14:43:29

试试这个:

代码语言:javascript
复制
    public class CalendarAdapter extends BaseAdapter {
        private List<Date> dateArray = new ArrayList();
        private Context mContext;
        private DateManager mDateManager;
        private LayoutInflater mLayoutInflater;
    
        //カスタムセルを拡張したらここでWigetを定義
        private static class ViewHolder {
            public TextView dateText;
        }
    
        public CalendarAdapter(Context context){
            mContext = context;
            mLayoutInflater = LayoutInflater.from(mContext);
            mDateManager = new DateManager();
            dateArray = mDateManager.getDays();
        }
    
        @Override
        public int getCount() {
            return dateArray.size();
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = mLayoutInflater.inflate(R.layout.calendar_cell, null);
                holder = new ViewHolder();
                holder.dateText = convertView.findViewById(R.id.dateText);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder)convertView.getTag();
            }
    
            //セルのサイズを指定
            float dp = mContext.getResources().getDisplayMetrics().density;
            AbsListView.LayoutParams params = new AbsListView.LayoutParams(parent.getWidth()/7 - (int)dp, 
            (parent.getHeight() - (int)dp * mDateManager.getWeeks() ) / mDateManager.getWeeks());
            convertView.setLayoutParams(params);
    
            //日付のみ表示させる
            SimpleDateFormat dateFormat = new SimpleDateFormat("d", Locale.US);
            holder.dateText.setText(dateFormat.format(dateArray.get(position)));
    
            //当月以外のセルをグレーアウト
            if (mDateManager.isCurrentMonth(dateArray.get(position))){
                convertView.setBackgroundColor(Color.WHITE);
            }else {
                convertView.setBackgroundColor(Color.LTGRAY);
            }
    
            //日曜日を赤、土曜日を青に color used for weekends
            int colorId;
            switch (mDateManager.getDayOfWeek(dateArray.get(position))){
                case 1:
                    colorId = Color.RED;
                    break;
                case 7:
                    colorId = Color.BLUE;
                    break;
    
                default:
                    colorId = Color.BLACK;
                    break;
            }
            holder.dateText.setTextColor(colorId);

convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Your code here
            }
        });
    
            return convertView;
        }
    
        @Override
        public long getItemId(int position) {
            return 0;
        }
    
        @Override
        public Object getItem(int position) {
            return null;
        }
    
        //表示月を取得 display the current month
        public String getTitle(){
            SimpleDateFormat format = new SimpleDateFormat("yyyy.MM", Locale.US);
            return format.format(mDateManager.mCalendar.getTime());
        }
    
        //翌月表示 display the next month
        public void nextMonth(){
            mDateManager.nextMonth();
            dateArray = mDateManager.getDays();
            this.notifyDataSetChanged();
        }
    
        //前月表示 display the previous month
        public void prevMonth(){
            mDateManager.prevMonth();
            dateArray = mDateManager.getDays();
            this.notifyDataSetChanged();
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64365765

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档