我只想制作这段代码,我有一个按钮,但是当我单击该按钮时,我想要一个计时器启动..并且每个计时器滴答声都会在 main.xml 中创建一个按钮..我该怎么做?请帮帮我先生!
Button a = (Button) findViewById(R.id.button1);
a.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new CountDownTimer(5000,1000) {
@Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
//how to create a button here!
}
@Override
public void onFinish() {
// TODO Auto-generated method stub
}
}.start();
}
});
我试试这个..但是当我点击按钮时出现这个词(“不幸的是程序已停止”)
final int i=1;
Button a = (Button) findViewById(R.id.button1);
a.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new CountDownTimer(5000,1000) {
@Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
//how to create a button here!
LinearLayout linearLayout = (LinearLayout) findViewById(R.layout.main);
Button btn = new Button(NyaActivity.this);
btn.setId(i+1);
linearLayout.addView(btn);
btn.setText("Button"+(i+1));
final int index = i;
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("TAG", "The index is" + index);
}
});
}
@Override
public void onFinish() {
// TODO Auto-generated method stub
}
}.start();
}
});
}