我正在尝试创建一个具有多个按钮的应用程序,当按下每个按钮时,它将启动一个新的计时器并在分配给该按钮的文本视图上显示时间。我已经成功地使其中一个按钮创建了一个工作计时器,但是我无法使另一个按钮为不同的文本视图执行相同的操作。我不知道如何创建多个onCLick方法。
编辑:我正在尝试让狼群按钮与wratihsbutton做同样的事情,但是有一个单独的计时器,并将其显示在不同的文本视图上。这是我到目前为止所做的:
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Handler.Callback;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView textView5, textView6, textView7;
long starttime = 0;
//this posts a message to the main thread from our timertask
//and updates the textfield
final Handler h = new Handler(new Callback() {
@Override
public boolean handleMessage(Message msg) {
long millis = System.currentTimeMillis() - starttime;
int seconds = (int) (millis / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
textView5.setText(String.format("%d:%02d", minutes, seconds));
return false;
}
});
//runs without timer be reposting self
Handler h2 = new Handler();
Runnable run = new Runnable() {
@Override
public void run() {
long millis = System.currentTimeMillis() - starttime;
int seconds = (int) (millis / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
textView5.setText(String.format("%d:%02d", minutes, seconds));
h2.postDelayed(this, 500);
if(seconds >= 10 || minutes >= 1){
textView5.setText("Ready");
}
}
};
//tells handler to send a message
class firstTask extends TimerTask {
@Override
public void run() {
h.sendEmptyMessage(0);
}
};
//tells activity to run on ui thread
class secondTask extends TimerTask {
@Override
public void run() {
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
long millis = System.currentTimeMillis() - starttime;
int seconds = (int) (millis / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
textView6.setText(String.format("%d:%02d", minutes, seconds));
}
});
}
};
class wolvesFirstTask extends TimerTask {
@Override
public void run() {
h.sendEmptyMessage(0);
}
};
//tells activity to run on ui thread
class wolvesSecondTask extends TimerTask {
@Override
public void run() {
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
long millis = System.currentTimeMillis() - starttime;
int seconds = (int) (millis / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
textView6.setText(String.format("%d:%02d", minutes, seconds));
}
});
}
};
Timer wraiths = new Timer();
Timer wolves = new Timer();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView5 = (TextView)findViewById(R.id.textView5);
textView6 = (TextView)findViewById(R.id.textView5);
Button wraithsbutton = (Button)findViewById(R.id.wraithsb);
Button wolvesbutton = (Button)findViewById(R.id.wolvesb);
wraithsbutton.setText("Start");
wraithsbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Button wraithsbutton = (Button)v;
if(wraithsbutton.getText().equals("Stop")){
wraiths.cancel();
wraiths.purge();
h2.removeCallbacks(run);
wraithsbutton.setText("Start");
}else{
starttime = System.currentTimeMillis();
wraiths = new Timer();
wraiths.schedule(new firstTask(), 0,500);
wraiths.schedule(new secondTask(), 0,500);
h2.postDelayed(run, 0);
wraithsbutton.setText("Stop");
}
}
});
}
}
答案 0 :(得分:2)
扩展类后需要添加implement关键字 然后你必须像这样重写onClick方法
public class Example extends Activity implements OnClickListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menulayout);
Button button1 = (Button) findViewById(R.id.b1);
button1.setOnClickListener(this);
Button button2 = (Button) findViewById(R.id.b2);
button2.setOnClickListener(this);
Button button3 = (Button) findViewById(R.id.b3);
button3.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v.getId() == R.id.b1)
{
//do something here if button1 is clicked
}
else if (v.getId() == R.id.b2)
{
//do something here if button2 is clicked
}
else if (v.getId() == R.id.b3)
{
//do something here if button3 is clicked
}
}
}
答案 1 :(得分:1)
无关的建议;我将时间显示拉出到一个方法中,因为相同的代码在4个位置。
private void showElapsedTime(long elapsedTime, TextView textView) {
int seconds = (int) (elapsedTime / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
textView.setText(String.format("%d:%02d", minutes, seconds));
}
private void showElapsedTimeOnUiThread(final long elapsedTime, final TextView textView) {
runOnUiThread(new Runnable() {
@Override
public void run() {
showElapsedTime(elapsedTime, textView);
}
});
}
从4个地点调用这些方法。
E.g。
Runnable run = new Runnable() {
@Override
public void run() {
showElapsedTime(System.currentTimeMillis() - starttime, textView5);
和
class secondTask extends TimerTask {
@Override
public void run() {
showElapsedTimeOnUiThread(System.currentTimeMillis() - starttime, textView6));
}
例如。
答案 2 :(得分:0)
基本上是这样的:
public class MyButton extends Button implements View.OnClickListener{
private TextView tv; /// or ArrayList<TextView> tvs;
public MyButton(Context context,TextView tv) {
super(context);
this.tv = tv; ///your text views to be set make as many as you want or pass an array of text views if you like List<? extends TextView> as a constructor param
setOnClickListener(this);
}
public void onClick(View view) {
//run your tasks here
//set the textviews as you like -
}
public MyButton(Context context, AttributeSet attrs) {
super(context, attrs);
setOnClickListener(this);
}
public MyButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setOnClickListener(this);
}
}
希望它会给你一些想法