1

当我把它放在我的 layout.xml

<CalendarView
    android:id="@+id/calendarView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

我的活动停止工作,所以当我单击按钮打开它时,设备被挂起。

我的活动代码

public class Reservation extends Activity {

    private Button AddReservation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reservation);
        AddReservation  = (Button)  findViewById(R.id.AddReservation);
        AddReservation.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {    
            finish();                   
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.reservation, menu);
        return true;
    }

}
4

1 回答 1

0

使用自定义日历

在你的活动中

 String date="";
GridView grid;
ImageView left,right;
TextView mon,yr;
MyCalendarAdapter adapter2;
ArrayList<String> weeks = new ArrayList<>();

String[] months= {"JANUARY","FEBRUARY","MARCH","APRIL","MAY","JUNE","JULY","AUGUST","SEPTEMBER","OCTOBER",
        "NOVEMBER","DECEMBER"};
private int month,year,current_month,current_year;
Date d;
Calendar cal;
private String dayOfTheWeek;
@SuppressLint("ResourceType")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dynamic_history);

    weeks.add("SUN");
    weeks.add("MON");
    weeks.add("TUE");
    weeks.add("WED");
    weeks.add("THU");
    weeks.add("FRI");
    weeks.add("SAT");

    grid =  findViewById(R.id.grid);
    left =  findViewById(R.id.left);
    right =  findViewById(R.id.right);
    mon =  findViewById(R.id.mon);
    yr =  findViewById(R.id.yr);

    cal = Calendar.getInstance();
    month = cal.get(Calendar.MONTH);
    year = cal.get(Calendar.YEAR);

    current_month = cal.get(Calendar.MONTH);
    current_year = cal.get(Calendar.YEAR);

    final SimpleDateFormat sdf = new SimpleDateFormat("EEE");
    d = new Date();
    d.setDate(1);
    dayOfTheWeek = sdf.format(d).toUpperCase();
    Log.d("FIRSTDAY",""+dayOfTheWeek);

    adapter2= new MyCalendarAdapter(cal,weeks.indexOf(dayOfTheWeek));
    grid.setAdapter(adapter2);
    mon.setText(months[month]);
    yr.setText(""+year);

    grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            int pos= (Integer) view.getTag();

            adapter2.notifyDataSetChanged();
        }
    });

     left.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(month==0)
            {
                month=11;
                year--;
            }
            else {
                month--;
            }
            d.setMonth(month);
            d.setYear(year);
            cal.set(Calendar.MONTH,month);
            cal.set(Calendar.YEAR,year);
            mon.setText(months[month]);
            yr.setText(""+year);
            dayOfTheWeek = sdf.format(d).toUpperCase();
            Log.d("FIRSTDAY",""+dayOfTheWeek);
            int w= weeks.indexOf(dayOfTheWeek);
            if(w==0)
            {
                adapter2= new MyCalendarAdapter(cal,6);
            }
            else {
                adapter2= new MyCalendarAdapter(cal,w-1);
            }
            grid.setAdapter(adapter2);

        }
    });

    right.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

                if (month == 11) {
                    month = 0;
                    year++;
                } else {
                    month++;
                }

                d.setMonth(month);
                d.setYear(year);
                cal.set(Calendar.MONTH, month);
                cal.set(Calendar.YEAR, year);
                mon.setText(months[month]);
                yr.setText("" + year);
                dayOfTheWeek = sdf.format(d).toUpperCase();
                Log.d("FIRSTDAY", "" + dayOfTheWeek);
                int w = weeks.indexOf(dayOfTheWeek);
                if (w == 0) {
                    adapter2 = new MyCalendarAdapter(cal, 6);
                } else {
                    adapter2 = new MyCalendarAdapter(cal, w - 1);
                }
                grid.setAdapter(adapter2);
            }

    });

}

活动.xml

<LinearLayout
    android:paddingTop="@dimen/_8sp"
    android:paddingBottom="@dimen/_40sp"
    android:paddingRight="@dimen/_14sp"
    android:paddingLeft="@dimen/_16sp"
    android:background="@drawable/white_bg"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
    <LinearLayout
        android:background="@color/colorPrimary"
        android:gravity="center"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="@dimen/_40sp"
        >
        <ImageView
            android:id="@+id/left"
            android:padding="@dimen/_8sp"
            android:src="@drawable/left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <LinearLayout
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="horizontal">
            <TextView
                android:paddingRight="@dimen/_5sp"
                android:id="@+id/mon"
                android:gravity="center_vertical|right"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:textColor="@color/white"
                android:textSize="@dimen/_14sp"
                android:textStyle="bold"
                android:text=""/>
            <TextView
                android:paddingLeft="@dimen/_5sp"
                android:id="@+id/yr"
                android:gravity="center_vertical"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:textColor="@color/white"
                android:textSize="@dimen/_14sp"
                android:textStyle="bold"
                android:text=""/>
        </LinearLayout>

        <ImageView
            android:id="@+id/right"
            android:padding="@dimen/_8sp"
            android:src="@drawable/right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <LinearLayout
        android:paddingTop="@dimen/_5sp"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <TextView
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textColor="@color/blue"
            android:textSize="@dimen/_14sp"
            android:textStyle="bold"
            android:text="SUN"/>
        <TextView
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textColor="@color/blue"
            android:textSize="@dimen/_14sp"
            android:textStyle="bold"
            android:text="MON"/>
        <TextView
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textColor="@color/blue"
            android:textSize="@dimen/_14sp"
            android:textStyle="bold"
            android:text="TUE"/>
        <TextView
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textColor="@color/blue"
            android:textSize="@dimen/_14sp"
            android:textStyle="bold"
            android:text="WED"/>
        <TextView
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textColor="@color/blue"
            android:textSize="@dimen/_14sp"
            android:textStyle="bold"
            android:text="THU"/>
        <TextView
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textColor="@color/blue"
            android:textSize="@dimen/_14sp"
            android:textStyle="bold"
            android:text="FRI"/>
        <TextView
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textColor="@color/blue"
            android:textSize="@dimen/_14sp"
            android:textStyle="bold"
            android:text="SAT"/>
    </LinearLayout>
    <GridView
        android:padding="@dimen/_5sp"
        android:id="@+id/grid"
        android:numColumns="7"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></GridView>
    <!--<android.support.v4.view.ViewPager-->

    <!--android:id="@+id/pager"-->
    <!--android:layout_width="match_parent"-->
    <!--android:layout_height="match_parent"-->
    <!--android:layout_weight="1"></android.support.v4.view.ViewPager>-->
</LinearLayout>

日历适配器

   public class MyCalendarAdapter extends BaseAdapter {
   Calendar c;

    int ct;
    public MyCalendarAdapter(Calendar c, int ct) {
        this.c = c;
        this.ct= ct;

        Log.d("FIRSTDAY",""+ct);
    }

    @Override
    public int getCount() {

        int x=getMonthDays(c.get(Calendar.MONTH)+1,c.get(Calendar.YEAR))+ct;
//        Log.d("FIRSTDAY",""+x);
        return x;
    }

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

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

    @Override
    public View getView(int p, View convertView, ViewGroup parent) {
        LayoutInflater mLayoutInflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = mLayoutInflater.inflate(R.layout.my_calendar, parent, false);
        TextView text = v.findViewById(R.id.text);

        int position= p+1-ct;
        if(position>0) {
            v.setTag(position);
            text.setText("" + position);
        }
        return v;
    }
    public static int getMonthDays(int month, int year) {
        int daysInMonth ;
        if (month == 4 || month == 6 || month == 9 || month == 11) {
            daysInMonth = 30;
        }
        else {
            if (month == 2) {
                daysInMonth = (year % 4 == 0) ? 29 : 28;
            } else {
                daysInMonth = 31;
            }
        }
//        Log.d("FIRSTDAY","days "+daysInMonth);
        return daysInMonth;
    }
}
于 2019-03-05T05:56:45.670 回答