【问题标题】:Changing button actions更改按钮操作
【发布时间】:2013-09-23 18:32:23
【问题描述】:

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

}

【问题讨论】:

  • 如果您告诉我们您的最终目标,那么我们可能会找到更简单的方法。你正在做的事情听起来可能会变得比它需要的更复杂。
  • 我的最终目标是我希望主类中的按钮发生变化。按钮文本应更改为在行程停止时开始,在行程开始时停止。所以我想更改按钮的文本以及按钮使用的 Intent。

标签: android eclipse button


【解决方案1】:

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

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

And here is the full docs for SharedPreference

【讨论】:

  • 好的,感谢您的快速回答。我是否像在我的代码中那样更改按钮的意图?还是我应该使用另一种方法?
  • 如果你这样做,那么你不需要对Intents 做任何事情,除了启动一个Activity。然后您只需检查AcitivtyonResume() 以查看SharedPreference 的值并相应地设置您的Button 文本。
猜你喜欢
  • 1970-01-01
  • 2017-03-17
  • 1970-01-01
  • 2017-03-27
  • 2013-11-03
  • 1970-01-01
  • 1970-01-01
  • 2016-03-02
  • 2015-08-07
相关资源
最近更新 更多