0

嘿,我想更改我的按钮操作。我有 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;
    }
}

}
4

1 回答 1

0

由于即使程序关闭,您也想保留这些值,我建议您使用SharedPreferences. 您可以在单击时将值设置为“停止”,然后在按下另一个值或需要Button时将其更改回来。然后,您可以在要更改文本的类中Button检查此值。onResume()Button

SharedPreferences Docs很好地解释了如何创建、读取和编辑SharedPreference值。

这是 SharedPreference 的完整文档

于 2013-09-23T18:59:23.630 回答