该应用程序具有初始消息屏幕,该屏幕在第一次运行时显示,其中使用datepicker从用户获取日期,然后从下一次直接转到main.xml。这是我试过的逻辑 在onCreate()
if(<date set>)
{
<open main.xml>
<listeners>
}
else
{
<get date from user>
<set flag>
setContentView(R.layout.initial_msg);
<make changes in main.xml according to date>
}
问题是它第一次执行它获取日期但是没有加载监听器,我认为这是因为代码根本没有执行。当我将监听器放在if块之外时,我得到一个空指针异常。但是,当我关闭应用程序并再次启动时,它会正常运行,如果()阻止其他()。
public class pgactivity extends Activity
{
/** Called when the activity is first created. */
SharedPreferences prefs;
Calendar c=Calendar.getInstance();
Calendar tempDate;
TextView tv1,tv2;
Menu theMenu;
LayoutInflater li;
int week;
DatePickerDialog.OnDateSetListener d=new DatePickerDialog.OnDateSetListener()
{
//@override
public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth)
{
c.set(Calendar.DATE,dayOfMonth);
c.set(Calendar.MONTH,monthOfYear);
c.set(Calendar.YEAR,year);
if(checkValidity())
{
SharedPreferences.Editor editor=prefs.edit();
//set the flag that indicates concieved_date has been added
editor.putBoolean("concieved_date", true);
int datepref;
datepref=c.get(Calendar.DATE);
datepref=datepref*1000000;
datepref+=c.get(Calendar.MONTH)*10000;
datepref+=c.get(Calendar.YEAR);
editor.putInt("date",datepref);
editor.commit();
setContentView(R.layout.main);
setData();
}
}
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//check if concieved date is set from the prefs file "app_data"
//if yes open application page else open initial message screen to set the date
prefs=getApplicationContext().getSharedPreferences("app_data",Context.MODE_APPEND);
if(prefs.getBoolean("concieved_date", false))
{
setContentView(R.layout.main);
setData();
//Listener for the temp button 'false'
//It resets the flag used to indicate if date is set or not. used for testing purpose
Button btn1=(Button)findViewById(R.id.setfalse);
btn1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
SharedPreferences.Editor editor=prefs.edit();
editor.putBoolean("concieved_date", false);
editor.commit();
TextView tv3=(TextView)findViewById(R.id.test);
tv3.setText("entered listener");
}
});
//Listener for the weekly_tip text view
//when clicked open the layout giving the full description
TextView tv2=(TextView)findViewById(R.id.weekly_tip);
tv2.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent j=new Intent(pgactivity.this,weekly.class);
Bundle b=new Bundle();
b.putInt("cur_week",week);
j.putExtras(b);
startActivity(j);
}
});
//Listener for open_remainders button to switch to Remainders page
Button btn2=(Button)findViewById(R.id.open_remainders);
btn2.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent i=new Intent(pgactivity.this,RemaindersPage.class);
startActivity(i);
}
});
}
else
{
setContentView(R.layout.initial_msg);
//click listener for textview 2
TextView tv1=(TextView)findViewById(R.id.init_msg);
tv1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
SharedPreferences.Editor editor=prefs.edit();
editor.putBoolean("concieved_date", true);
editor.commit();
new DatePickerDialog(pgactivity.this,d,
c.get(Calendar.YEAR),
c.get(Calendar.MONTH),
c.get(Calendar.DAY_OF_MONTH)).show();
}
});
}
}
void setData()
{
Long sec,tempSec;
int date,day,month,year;
date=prefs.getInt("date", 10101010);
day=date/1000000;
date=date%1000000;
month=date/10000;
date=date%10000;
year=date;
c.set(year, month, day);
tempDate=c;
//insert value to concieved_date textfield
tv1=(TextView)findViewById(R.id.concieved_date);
tv1.setText(android.text.format.DateFormat.format("dd MMM yyyy", tempDate));
//insert value to delivery_date
tv1=(TextView)findViewById(R.id.delivery_date);
tempDate.add(Calendar.DATE, 280);
tv1.setText(android.text.format.DateFormat.format("dd MMM yyyy", tempDate));
//insert value to days_to_delivery
tv1=(TextView)findViewById(R.id.days_to_delivery);
c=Calendar.getInstance(); //c has current date
sec=tempDate.getTimeInMillis()-c.getTimeInMillis(); //find diff in millisecs
sec=sec/86400000;
tv1.setText(sec.toString());
//insert value for days_into_pregnancy
tv1=(TextView)findViewById(R.id.days_into_pregnancy);
tempSec=280-sec;
tv1.setText(tempSec.toString());
//insert value for current_week
tv1=(TextView)findViewById(R.id.current_week);
tempSec=tempSec/7;
week=tempSec.intValue();
tv1.setText(tempSec.toString());
}
//user method to check the date validity : check if date entered is a reasonable value
boolean checkValidity()
{
Long duration;
tempDate=Calendar.getInstance(); //get today's date in c
//check if user date is on or before today's date
if(tempDate.before(c))
{
AlertDialog.Builder ad=new AlertDialog.Builder(this);
ad.setMessage("Specified date should be on or before today's date");
ad.setNeutralButton("OK",new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface arg0,int arg1){}
}
);
ad.show();
return false;
}
//check if diff between user date and todays date is more than 280 days
duration=tempDate.getTimeInMillis()-c.getTimeInMillis();
duration/=86400000;
if(duration>280)
{
AlertDialog.Builder ad=new AlertDialog.Builder(this);
ad.setMessage("Specified date can be atmost 280 days before today's date");
ad.setNeutralButton("OK",new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface arg0,int arg1){}
}
).show();
return false;
}
return true;
}
}
答案 0 :(得分:1)
如果您需要else
- 块中的听众,也可以将此代码放在if
- 块之前。
如果执行if
或else
时布局发生变化,您可以使用onClick
-attribute in your XML-Layout定义。
要保存数据,您可以使用SharedPreferences。