1

我正在网格视图中制作日历,并通过R.layout.screen_gridcell.

日历正常膨胀,但我想添加一个进一步的功能,当用户按下一天时,按下的单元格将使用drawable calendar_tile_chosen.

但是当我按下任何单元格时什么都没有发生。

问题:

  1. onClick当我可以应用效果时,如何取回所选网格单元按钮的 id drawable calendar_tile_chosen
  2. 由于用户可能会在 day1 之后点击另一天(day2),当按下 day2 时,如何将 day1 设置回正常布局?在将可绘制的 calendar_tile_chosen 应用于第 2 天之前,需要记住第 1 天并刷新整个日历吗?这条路线似乎很笨拙..

查看getView:

    @Override
    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);
        gridcell.setTag(theday + "-" + themonth + "-" + theyear);

点击:

    @Override
    public void onClick(View view) 
    {
        String date_month_year = (String) view.getTag();
        gridcell.setBackgroundResource(R.drawable.calendar_tile_chosen); 

screen_gridcell.xml

<Button
    android:id="@+id/calendar_day_gridcell"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@drawable/calendar_tile"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#FFFFFF" >
</Button>

<TextView
    android:id="@+id/num_events_per_day"
    style="@style/calendar_event_style"
    android:layout_width="10dip"
    android:layout_height="10dip"
    android:layout_gravity="right" >
</TextView>

calendar_tile.xml

<item android:state_pressed="true" >         
    <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
        <stroke android:width="1dp" android:color="@color/black" />
        <solid android:color="@color/orange"/>
        <padding android:left="1dp" android:top="1dp" 
            android:right="1dp" android:bottom="1dp" /> 
    </shape>    
</item>

<item>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
        <stroke android:width="1dp" android:color="@color/black" />
        <solid android:color="@color/white"/>
        <padding android:left="1dp" android:top="1dp" 
            android:right="1dp" android:bottom="1dp" /> 
    </shape>
</item>

4

0 回答 0