gpt4 book ai didi

android - 在我的代码中将 clickListeners 放在哪里?

转载 作者:行者123 更新时间:2023-11-30 04:36:55 25 4
gpt4 key购买 nike

该应用程序具有初始消息屏幕,该屏幕在首次运行时显示,其中使用日期选择器从用户处获取日期,然后从下一次开始直接转到 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 block 之外时,我得到一个空指针异常。但是当我关闭应用程序并再次启动它时,它会像这次打开 if() block 而不是 else() 一样工作。

 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;
}

最佳答案

如果您也需要在 else block 中使用您的监听器,您可以将此代码放在 if block 之前。

如果在 ifelse 执行后布局发生变化,您可以使用 onClick-attribute in your XML-Layout定义。

要保存您的数据,您可以使用 SharedPreferences .

关于android - 在我的代码中将 clickListeners 放在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6691341/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com