嘿,我想改变我的按钮动作。我有3节课。当我在开始时单击starttur时,我想要更改主类中的开始按钮以停止,并且还将按钮的意图更改为停止类,此时按钮不起作用,我认为这是因为是还没有开始或停止的声明,但我如何制作第一个循环标志?
如果用户退出应用程序并再次启动应用程序,这是否有效,应用程序会记住最后设置的语句吗?
这是我的主要课程:
public class Main extends Activity{
Button bStart, bStop;
TextView tvDate, tvKm;
Spinner spinner1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bStart = (Button) findViewById(R.id.bStart);
tvDate = (TextView) findViewById(R.id.tvDate);
tvKm = (TextView) findViewById(R.id.tvKm);
spinner1 = (Spinner) findViewById(R.id.spinner1);
if (menu.getStringExtra("start") != null)
{
String start = menu.getStringExtra("start");
if (start.equals("started"))
{
bStart.setText("Stop");
bStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent stop = new Intent("com.uniqueapps.runner.STOP");
startActivity(stop);
}
});
}
if (start.equals("stopped"))
{
bStart.setText("Start");
bStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent start = new Intent("com.uniqueapps.runner.START");
startActivity(start);
}
});
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
这是我的开始课程:
public class Start extends Main implements OnClickListener {
Button bStartTur;
EditText etDate, etKm;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.start);
bStartTur = (Button) findViewById(R.id.bStartTur);
bStartTur.setOnClickListener(this);
etDate = (EditText) findViewById(R.id.etdate);
etKm = (EditText) findViewById(R.id.etKm);
Calendar cal = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
etDate.setText(format.format(new Date()));
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bStartTur:
Intent menu = new Intent("com.uniqueapps.runner.MENU");
menu.putExtra("start", "started");
startActivity(menu);
break;
}
}
}
这是我的停课:
public class Stop extends Main implements OnClickListener {
Button bStop;
EditText locations, kilometer;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.stop);
bStop = (Button) findViewById(R.id.bstopTur);
bStop.setOnClickListener(this);
locations = (EditText) findViewById(R.id.locations);
kilometer = (EditText) findViewById(R.id.Kilometer);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.bstopTur:
Intent menu = new Intent("com.uniqueapps.runner.MENU");
menu.putExtra("start", "stopped");
startActivity(menu);
break;
}
}
}
答案 0 :(得分:0)
由于您想要在程序关闭时保留值,我建议您使用SharedPreferences
。您可以在单击Button
时将值设置为“停止”,然后在按下另一个Button
时或在需要时将其更改回来。然后,您可以在要更改onResume()
文本的班级的Button
中查看此值。
SharedPreferences Docs很好地解释了如何创建,阅读和编辑SharedPreference
值。