我夸大了一个视图,我在日历日的每个视图上设置了文本,但是当我尝试递增或递减日期并再次设置文本或骑过视图时,它不起作用,在切换的情况下我想要增加或减少并显示该特定月份的整个日历
package com.vedicrishiastro.kundli.Screens;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView;
import android.support.v7.widget.Toolbar;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.vedicrishiastro.kundli.MainActivity;
import com.vedicrishiastro.kundli.R;
import com.vedicrishiastro.kundli.datasets.Const;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import mehdi.sakout.fancybuttons.FancyButton;
import static android.support.v4.content.res.TypedArrayUtils.getResourceId;
public class PanchangCal extends AbstractActivity implements View.OnClickListener {
LinearLayout[] linearday=new LinearLayout[42];
View[] myView=new View[42];
private CardView cardprevious,cardnext,cardcurrent;
private TextView date;
TextView datetext;
private FancyButton location;
private int CITY=101;
RelativeLayout inflatedLayout;
Double changedPlaceLong;
Double changedPlaceLat;
Double changedPlacetz;
Calendar cal=Calendar.getInstance();
Calendar changeday=Calendar.getInstance();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_panchang_cal);
date=(TextView)findViewById(R.id.todaydate);
cardprevious=(CardView)findViewById(R.id.cardpprevious);
cardnext=(CardView)findViewById(R.id.cardppnext);
cardcurrent=(CardView)findViewById(R.id.cardpptoday);
location=(FancyButton)findViewById(R.id.btnCityD);
String[] MONTHS=getResources().getStringArray(R.array.month);
int mm=cal.get(Calendar.MONTH);
int yy=cal.get(Calendar.YEAR);
date.setText(new StringBuilder().append(MONTHS[mm]).append("-").append(yy));
for (int i = 0; i < 42; i++) {
int resid = getResources().getIdentifier("cald" + i, "id", getPackageName());
linearday[i] = (LinearLayout) findViewById(resid);
}
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// calculae length of month
changeday.set(Calendar.DAY_OF_MONTH,1);
int monthLength = changeday.getInstance().getActualMaximum(Calendar.DAY_OF_MONTH);
int dayOfFirstWeek = changeday.get(Calendar.DAY_OF_WEEK);
int date = 1;
for (int j = 0; j < 42; j++)
{
if( j+1 < dayOfFirstWeek || date > monthLength)
{
myView[j] = inflater.inflate(R.layout.activity_panchang_cal_inflate, null);
}
else
{
myView[j] = inflater.inflate(R.layout.activity_panchang_cal_inflate, null);
datetext = (TextView) myView[j].findViewById(R.id.panchangcaltxt10);
datetext.setText(new StringBuilder().append(date));
date++;
}
}
for(int k=0;k<42;k++){
linearday[k].addView(myView[k]);
}
for(int l=0;l<42;l++){
linearday[l].setOnClickListener(this);
}
cardprevious.setOnClickListener(this);
cardnext.setOnClickListener(this);
location.setOnClickListener(this);
}
public void onClick(View v)
{int id=v.getId();
switch (id){
case R.id.cald0:{Toast.makeText(PanchangCal.this, "Day:1", Toast.LENGTH_SHORT).show();}break;
case R.id.cald1:{Toast.makeText(PanchangCal.this, "Day:2", Toast.LENGTH_SHORT).show();}break;
case R.id.cald2:{Toast.makeText(PanchangCal.this, "Day:3", Toast.LENGTH_SHORT).show();}break;
case R.id.cald3:{Toast.makeText(PanchangCal.this, "Day:4", Toast.LENGTH_SHORT).show();}break;
case R.id.cald4:{Toast.makeText(PanchangCal.this, "Day:5", Toast.LENGTH_SHORT).show();}break;
case R.id.cald5:{Toast.makeText(PanchangCal.this, "Day:6", Toast.LENGTH_SHORT).show();}break;
case R.id.cald6:{Toast.makeText(PanchangCal.this, "Day:7", Toast.LENGTH_SHORT).show();}break;
case R.id.cald7:{Toast.makeText(PanchangCal.this, "Day:8", Toast.LENGTH_SHORT).show();}break;
case R.id.cald8:{Toast.makeText(PanchangCal.this, "Day:9", Toast.LENGTH_SHORT).show();}break;
case R.id.cald9:{Toast.makeText(PanchangCal.this, "Day:10", Toast.LENGTH_SHORT).show();}break;
case R.id.cald10:{Toast.makeText(PanchangCal.this, "Day:11", Toast.LENGTH_SHORT).show();}break;
case R.id.cald11:{Toast.makeText(PanchangCal.this, "Day:12", Toast.LENGTH_SHORT).show();}break;
case R.id.cald12:{Toast.makeText(PanchangCal.this, "Day:13", Toast.LENGTH_SHORT).show();}break;
case R.id.cald13:{Toast.makeText(PanchangCal.this, "Day:14", Toast.LENGTH_SHORT).show();}break;
case R.id.cald14:{Toast.makeText(PanchangCal.this, "Day:15", Toast.LENGTH_SHORT).show();}break;
case R.id.cald15:{Toast.makeText(PanchangCal.this, "Day:16", Toast.LENGTH_SHORT).show();}break;
case R.id.cald16:{Toast.makeText(PanchangCal.this, "Day:17", Toast.LENGTH_SHORT).show();}break;
case R.id.cald17:{Toast.makeText(PanchangCal.this, "Day:18", Toast.LENGTH_SHORT).show();}break;
case R.id.cald18:{Toast.makeText(PanchangCal.this, "Day:19", Toast.LENGTH_SHORT).show();}break;
case R.id.cald19:{Toast.makeText(PanchangCal.this, "Day:20", Toast.LENGTH_SHORT).show();}break;
case R.id.cald20:{Toast.makeText(PanchangCal.this, "Day:21", Toast.LENGTH_SHORT).show();}break;
case R.id.cald21:{Toast.makeText(PanchangCal.this, "Day:22", Toast.LENGTH_SHORT).show();}break;
case R.id.cald22:{Toast.makeText(PanchangCal.this, "Day:23", Toast.LENGTH_SHORT).show();}break;
case R.id.cald23:{Toast.makeText(PanchangCal.this, "Day:24", Toast.LENGTH_SHORT).show();}break;
case R.id.cald24:{Toast.makeText(PanchangCal.this, "Day:25", Toast.LENGTH_SHORT).show();}break;
case R.id.cald25:{Toast.makeText(PanchangCal.this, "Day:26", Toast.LENGTH_SHORT).show();}break;
case R.id.cald26:{Toast.makeText(PanchangCal.this, "Day:27", Toast.LENGTH_SHORT).show();}break;
case R.id.cald27:{Toast.makeText(PanchangCal.this, "Day:28", Toast.LENGTH_SHORT).show();}break;
case R.id.cald28:{Toast.makeText(PanchangCal.this, "Day:29", Toast.LENGTH_SHORT).show();}break;
case R.id.cald29:{Toast.makeText(PanchangCal.this, "Day:30", Toast.LENGTH_SHORT).show();}break;
case R.id.cald30:{Toast.makeText(PanchangCal.this, "Day:31", Toast.LENGTH_SHORT).show();}break;
case R.id.cald31:{Toast.makeText(PanchangCal.this, "Day:32", Toast.LENGTH_SHORT).show();}break;
case R.id.cald32:{Toast.makeText(PanchangCal.this, "Day:33", Toast.LENGTH_SHORT).show();}break;
case R.id.cald33:{Toast.makeText(PanchangCal.this, "Day:34", Toast.LENGTH_SHORT).show();}break;
case R.id.cald34:{Toast.makeText(PanchangCal.this, "Day:35", Toast.LENGTH_SHORT).show();}break;
case R.id.cald35:{Toast.makeText(PanchangCal.this, "Day:36", Toast.LENGTH_SHORT).show();}break;
case R.id.cald36:{Toast.makeText(PanchangCal.this, "Day:37", Toast.LENGTH_SHORT).show();}break;
case R.id.cald37:{Toast.makeText(PanchangCal.this, "Day:38", Toast.LENGTH_SHORT).show();}break;
case R.id.cald38:{Toast.makeText(PanchangCal.this, "Day:39", Toast.LENGTH_SHORT).show();}break;
case R.id.cald39:{Toast.makeText(PanchangCal.this, "Day:40", Toast.LENGTH_SHORT).show();}break;
case R.id.cald40:{Toast.makeText(PanchangCal.this, "Day:41", Toast.LENGTH_SHORT).show();}break;
case R.id.cald41:{Toast.makeText(PanchangCal.this, "Day:42", Toast.LENGTH_SHORT).show();}break;
case R.id.cardpprevious:{
String[] MONTHS=getResources().getStringArray(R.array.month);
cal.add(Calendar.MONTH,-1);
int mm=cal.get(Calendar.MONTH);
int yy=cal.get(Calendar.YEAR);
date.setText(new StringBuilder().append(MONTHS[mm]).append("-").append(yy));
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// calculae length of month
changeday.add(Calendar.MONTH,-1);
changeday.set(Calendar.DAY_OF_MONTH,1);
int monthLength = changeday.getInstance().getActualMaximum(Calendar.DAY_OF_MONTH);
int dayOfFirstWeek = changeday.get(Calendar.DAY_OF_WEEK);
int date = 1;
for (int j = 0; j < 42; j++)
{
if( j+1 < dayOfFirstWeek || date > monthLength)
{
myView[j] = inflater.inflate(R.layout.activity_panchang_cal_inflate, null);
}
else
{
myView[j] = inflater.inflate(R.layout.activity_panchang_cal_inflate, null);
datetext = (TextView) myView[j].findViewById(R.id.panchangcaltxt10);
datetext.setText(new StringBuilder().append(date));
date++;
}
}
}break;
case R.id.cardppnext:{
String[]MONTHS=getResources().getStringArray(R.array.month);
cal.add(Calendar.MONTH,1);
int mm=cal.get(Calendar.MONTH);
int yy=cal.get(Calendar.YEAR);
date.setText(new StringBuilder().append(MONTHS[mm]).append("-").append(yy));
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// calculae length of month
changeday.add(Calendar.MONTH,1);
changeday.set(Calendar.DAY_OF_MONTH,1);
int monthLength = changeday.getInstance().getActualMaximum(Calendar.DAY_OF_MONTH);
int dayOfFirstWeek = changeday.get(Calendar.DAY_OF_WEEK);
int date = 1;
for (int j = 0; j < 42; j++)
{
if( j+1 < dayOfFirstWeek || date > monthLength)
{
myView[j] = inflater.inflate(R.layout.activity_panchang_cal_inflate, null);
}
else
{
myView[j] = inflater.inflate(R.layout.activity_panchang_cal_inflate, null);
datetext = (TextView) myView[j].findViewById(R.id.panchangcaltxt10);
datetext.setText(new StringBuilder().append(date));
date++;
}
}
}break;
case R.id.btnCityD:{
startActivityForResult(new Intent(this, MainActivity.class),CITY);
}break;
}
}
public void onActivityResult(int requestCode,int resultCode,Intent intent){
super.onActivityResult(requestCode,resultCode,intent);
if(requestCode==CITY){
String geoName=intent.getStringExtra(Const.GeoConst.NAME);
location.setText(geoName);
}
}
}
答案 0 :(得分:0)
用于增加日历日期
cal.add(Calendar.DAY_OF_MONTH, 1);
减少:
cal.add(Calendar.DAY_OF_MONTH, -1);