显示与 TABLELAYOUT 相加

发布于 2024-10-13 04:12:38 字数 6368 浏览 6 评论 0原文

我正在使用表格布局创建日历作为屏幕显示。但是,当我需要用用户选择的新月份或年份来更新显示时,它只会将之前存储到表中的数据相加。

这是我的代码:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.main);

    table = new TableLayout(this);

    FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(         
            ViewGroup.LayoutParams.FILL_PARENT,         
            ViewGroup.LayoutParams.FILL_PARENT); 
    table.setLayoutParams(lp);
    table.setStretchAllColumns(true); 
    rowLp2 = new TableLayout.LayoutParams(         
            ViewGroup.LayoutParams.FILL_PARENT,         
            ViewGroup.LayoutParams.FILL_PARENT,         
            1.0f);
    cellLp2 = new TableRow.LayoutParams(         
            ViewGroup.LayoutParams.FILL_PARENT,         
            ViewGroup.LayoutParams.FILL_PARENT,         
            1.0f);
   // cellLp3 = new TableRow.LayoutParams(arg0, arg1)
setYYMMDD(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_WEEK));
    leadGap = new GregorianCalendar(yy, mm, 1).get(Calendar.DAY_OF_WEEK) - 1;
  //calling methods
    header();
    weeks();
    //setLeadGap(2011, 2);
    days(dom[mm]);


    setContentView(table);
}

private void days(int noDays)
{
    TableRow row = new TableRow(this);
    int d=1;
    nRow = (leadGap > 3) ? nRow+1 : nRow;
    //textMonth.setText(Integer.toString(nRow));
    for (int r = 0; r <nRow ; ++r) 
    {     
        row = new TableRow(this);
        if (r==0)
            {   int l = 0;
                for (int f = 1; f<7; ++f)
                {
                    if( l <leadGap)
                    {
                            TextView v = new TextView(this);
                            v.setText("");

                            row.addView(v, cellLp2);

                            l++;
                    }
                    if (l == leadGap)
                    {
                        Button v = new Button(this);
                        v.setText(Integer.toString(d));
                        row.addView(v, cellLp2);
                        d++;
                    }
                    row.setBackgroundColor(Color.rgb(55, 254, 55));

                }
            }
            else
            {

                for (int f = 0; f < 7; ++f)     
                {  

                        if (d <= noDays)
                        {
                            //Button btn = new Button(this);  
                            //btn.setText(Integer.toString(d));         
                            //row.addView(btn, cellLp2);
                            Button v = new Button(this);
                            v.setText(Integer.toString(d));
                            row.addView(v, cellLp2);

                            row.setBackgroundColor(Color.rgb(55, 254, 55));
                            d++;
                        }
                        else
                        {
                            TextView btn = new TextView(this);  
                            btn.setText("");         
                            row.addView(btn, cellLp2);

                        }


                }
            }
        table.addView(row, rowLp2); 
    } 

}

private void header(){
    headertbl= new TableLayout(this);
    btnMonthPos = new Button(this);
    btnMonthPos.setOnClickListener(new View.OnClickListener() 
    {            
        public void onClick(View v) 
        {   
            if (iMonth > 10)
            {
                iMonth = 0;
            }
            else
            {
                iMonth += 1;
            }
            setLeadGap(iYear, iMonth);
            days(dom[iMonth]);



            textMonth.setText(months[iMonth]);   
            //recompute();
        }


    });   
textMonth = new TextView(this);
btnMonthNeg = new Button(this);
    btnMonthNeg.setOnClickListener(new View.OnClickListener() 
    {            
        public void onClick(View v) 
        {   
            if (iMonth < 1)
            {
                iMonth = 11;
            }
            else
            {
                iMonth -= 1;
            }
            textMonth.setText(months[iMonth]);          
        }        
    });   
btnYearPos = new Button(this);
    btnYearPos.setOnClickListener(new View.OnClickListener() 
    {            
        public void onClick(View v) 
        {   
            iYear += 1;
            textYear.setText(Integer.toString(iYear));  
            leadGap = new GregorianCalendar(yy, mm, 1).get(Calendar.DAY_OF_WEEK) - 1;
            //sunBtn.setText(Integer.toString(leadGap));
        }        
    });
textYear = new TextView(this);
btnYearNeg = new Button(this);
    btnYearNeg.setOnClickListener(new View.OnClickListener() 
        {            
            public void onClick(View v) 
            {   
                iYear -= 1;
                textYear.setText(Integer.toString(iYear));          
            }        
        }); 

//setting values during loading
btnMonthPos.setText("++");
textMonth.setText(months[mm]);
btnMonthNeg.setText("--");
btnYearPos.setText("++");
textYear.setText(Integer.toString(calendar.get(Calendar.YEAR)));
btnYearNeg.setText("--");

TableRow headerRow = new TableRow(this);
headerRow.addView(btnMonthPos, cellLp2);
headerRow.addView(textMonth, cellLp2);
headerRow.addView(btnMonthNeg, cellLp2);
headerRow.addView(btnYearPos, cellLp2);
headerRow.addView(textYear, cellLp2);
headerRow.addView(btnYearNeg, cellLp2);

headertbl.addView(headerRow, rowLp2);

 table.addView(headertbl);

}

private void weeks()
{
    String[] weekName = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
    tblWeek = new TableLayout(this);
    TableRow rowWeek = new TableRow(this);

    for (int i = 0; i<1; i++)
    {
        rowWeek = new TableRow(this);
        for (int j=0; j<weekName.length;j++)
        {
            TextView day = new TextView(this);
            day.setText(" "+weekName[j]);
            day.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
            rowWeek.addView(day, cellLp2);
        }
        rowWeek.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
        tblWeek.addView(rowWeek, rowLp2);
    }

     table.addView(tblWeek);
}
}

I am creating a calendar using tablelayout as my display to the screen. However when I need to update the display with either new month or year being selected by the user, it only adds up to the previouse stored data into the table.

HERE is my code:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.main);

    table = new TableLayout(this);

    FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(         
            ViewGroup.LayoutParams.FILL_PARENT,         
            ViewGroup.LayoutParams.FILL_PARENT); 
    table.setLayoutParams(lp);
    table.setStretchAllColumns(true); 
    rowLp2 = new TableLayout.LayoutParams(         
            ViewGroup.LayoutParams.FILL_PARENT,         
            ViewGroup.LayoutParams.FILL_PARENT,         
            1.0f);
    cellLp2 = new TableRow.LayoutParams(         
            ViewGroup.LayoutParams.FILL_PARENT,         
            ViewGroup.LayoutParams.FILL_PARENT,         
            1.0f);
   // cellLp3 = new TableRow.LayoutParams(arg0, arg1)
setYYMMDD(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_WEEK));
    leadGap = new GregorianCalendar(yy, mm, 1).get(Calendar.DAY_OF_WEEK) - 1;
  //calling methods
    header();
    weeks();
    //setLeadGap(2011, 2);
    days(dom[mm]);


    setContentView(table);
}

private void days(int noDays)
{
    TableRow row = new TableRow(this);
    int d=1;
    nRow = (leadGap > 3) ? nRow+1 : nRow;
    //textMonth.setText(Integer.toString(nRow));
    for (int r = 0; r <nRow ; ++r) 
    {     
        row = new TableRow(this);
        if (r==0)
            {   int l = 0;
                for (int f = 1; f<7; ++f)
                {
                    if( l <leadGap)
                    {
                            TextView v = new TextView(this);
                            v.setText("");

                            row.addView(v, cellLp2);

                            l++;
                    }
                    if (l == leadGap)
                    {
                        Button v = new Button(this);
                        v.setText(Integer.toString(d));
                        row.addView(v, cellLp2);
                        d++;
                    }
                    row.setBackgroundColor(Color.rgb(55, 254, 55));

                }
            }
            else
            {

                for (int f = 0; f < 7; ++f)     
                {  

                        if (d <= noDays)
                        {
                            //Button btn = new Button(this);  
                            //btn.setText(Integer.toString(d));         
                            //row.addView(btn, cellLp2);
                            Button v = new Button(this);
                            v.setText(Integer.toString(d));
                            row.addView(v, cellLp2);

                            row.setBackgroundColor(Color.rgb(55, 254, 55));
                            d++;
                        }
                        else
                        {
                            TextView btn = new TextView(this);  
                            btn.setText("");         
                            row.addView(btn, cellLp2);

                        }


                }
            }
        table.addView(row, rowLp2); 
    } 

}

private void header(){
    headertbl= new TableLayout(this);
    btnMonthPos = new Button(this);
    btnMonthPos.setOnClickListener(new View.OnClickListener() 
    {            
        public void onClick(View v) 
        {   
            if (iMonth > 10)
            {
                iMonth = 0;
            }
            else
            {
                iMonth += 1;
            }
            setLeadGap(iYear, iMonth);
            days(dom[iMonth]);



            textMonth.setText(months[iMonth]);   
            //recompute();
        }


    });   
textMonth = new TextView(this);
btnMonthNeg = new Button(this);
    btnMonthNeg.setOnClickListener(new View.OnClickListener() 
    {            
        public void onClick(View v) 
        {   
            if (iMonth < 1)
            {
                iMonth = 11;
            }
            else
            {
                iMonth -= 1;
            }
            textMonth.setText(months[iMonth]);          
        }        
    });   
btnYearPos = new Button(this);
    btnYearPos.setOnClickListener(new View.OnClickListener() 
    {            
        public void onClick(View v) 
        {   
            iYear += 1;
            textYear.setText(Integer.toString(iYear));  
            leadGap = new GregorianCalendar(yy, mm, 1).get(Calendar.DAY_OF_WEEK) - 1;
            //sunBtn.setText(Integer.toString(leadGap));
        }        
    });
textYear = new TextView(this);
btnYearNeg = new Button(this);
    btnYearNeg.setOnClickListener(new View.OnClickListener() 
        {            
            public void onClick(View v) 
            {   
                iYear -= 1;
                textYear.setText(Integer.toString(iYear));          
            }        
        }); 

//setting values during loading
btnMonthPos.setText("++");
textMonth.setText(months[mm]);
btnMonthNeg.setText("--");
btnYearPos.setText("++");
textYear.setText(Integer.toString(calendar.get(Calendar.YEAR)));
btnYearNeg.setText("--");

TableRow headerRow = new TableRow(this);
headerRow.addView(btnMonthPos, cellLp2);
headerRow.addView(textMonth, cellLp2);
headerRow.addView(btnMonthNeg, cellLp2);
headerRow.addView(btnYearPos, cellLp2);
headerRow.addView(textYear, cellLp2);
headerRow.addView(btnYearNeg, cellLp2);

headertbl.addView(headerRow, rowLp2);

 table.addView(headertbl);

}

private void weeks()
{
    String[] weekName = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
    tblWeek = new TableLayout(this);
    TableRow rowWeek = new TableRow(this);

    for (int i = 0; i<1; i++)
    {
        rowWeek = new TableRow(this);
        for (int j=0; j<weekName.length;j++)
        {
            TextView day = new TextView(this);
            day.setText(" "+weekName[j]);
            day.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
            rowWeek.addView(day, cellLp2);
        }
        rowWeek.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
        tblWeek.addView(rowWeek, rowLp2);
    }

     table.addView(tblWeek);
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

塔塔猫 2024-10-20 04:12:38

在 week() day() 和其他函数添加新行到表(Tablelayout)之前,考虑删除旧的表行

Consider deleting the old tablerows before adding new rows in weeks() day() and other function to the table(Tablelayout)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文