gpt4 book ai didi

android - 如何在android中设置循环延迟?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:58:39 24 4
gpt4 key购买 nike

我正在尝试延迟显示乘法表。我的代码运行良好,但我无法实现延迟。

这是我的代码:

tableButton1 = (Button) findViewById(R.id.Button1);
tableButton1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
new Thread(new Runnable() {
public void run() {
str = tableButton1.getText().toString();
a = Integer.parseInt(str);
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= 10; i++){
sb.append(a + " x " + i + " = " + i * a+ "\n");

}
s = String.valueOf(sb);

Intent intent=new Intent(getApplicationContext(),TextViewActivity.class);
intent.putExtra("MA", s);
startActivity(intent);
}
});
//Toast.makeText(getApplicationContext(), "Hi" +ss, 222).show();
}
});

任何答案都是可观的。

提前致谢

更新代码:- 此代码在@theLittleNaruto 的帮助下工作

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class TestActivity extends Activity{
Button tableButton1;
TextView txtView;

int value = 0;
static int count = 0;
Handler handle = new Handler();

StringBuilder sb = new StringBuilder();

Runnable r = new Runnable() {

@Override
public void run() {
Toast.makeText(getApplicationContext(), "onrunnable" +sb, 222).show();
// TODO Auto-generated method stub
updateTable();
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.text_display);

Toast.makeText(getApplicationContext(), "oncreate" , 222).show();
txtView = (TextView) findViewById(R.id.outputTXT);
tableButton1 = (Button) findViewById(R.id.seven);
tableButton1.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

Toast.makeText(getApplicationContext(), "onclick" , 222).show();
value= Integer.parseInt(tableButton1.getText().toString());
updateTable();

}
});
}


public void updateTable(){

count+=1000;
if(count==11000){
//Toast.makeText(getApplicationContext(), "onupdate" , 222).show();
count = 0;
value=0;
handle.removeCallbacks(r);
sb.setLength(0);

}else{

sb.append(value + " x " + count/1000 + " = " + count/1000 * value+ "\n");
handle.postDelayed(r, 1000);
// Toast.makeText(getApplicationContext(), "onupdateElse" +sb, 222).show();
txtView.setText(sb);
}


}


}

感谢所有的支持者和他们尽最大努力帮助我

最佳答案

你为什么不花点力气试试别人说的 ;)

public class TestActivity extends Activity{

int value = 0;
static int count = 0;
Handler handle = new Handler();

StringBuilder sb = new StringBuilder();

Runnable r = new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
updateTable();
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.oaot_get);


tableButton1.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {


value= Integer.parseInt(tableButton1.getText().toString());
updateTable();

}
});
}


public void updateTable(){

count+=1000;
if(count==11000){

count = 0;
value=0;
handle.removeCallbacks(r);
sb.setLength(0);

}else{

sb.append(value + " x " + count/1000 + " = " + count/1000 * value+ "\n");
handle.postDelayed(r, 1000);

}


}


}

关于android - 如何在android中设置循环延迟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21328355/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com