[英]How can i stop sending messages which are sending in loop on click of an button in android
I want to know how can I stop message sending in a loop. 我想知道如何停止循环中的消息发送。
When I press stop, messages are still sent. 当我按停止键时,仍会发送消息。
I have provided a stop()
method. 我提供了
stop()
方法。
for (i = 0; i < copy; i++) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if(stop!=1)
sms.sendTextMessage(number,null,message,sentPIn,pdelint);
}
}, 25000);
stop method
int stop=0;
public void st(View view){
Button b=(Button)findViewById(R.id.button2);
b.setText("Stopped");
stop=1;
}
Modify St function to this 修改St函数对此
public void st() {
Button b = (Button) findViewById(R.id.button);
@Override
b.setOnClickListener(new View.OnClickListener() {
b.setText("Stopped!");
stop=1;
});
}
The problem was u were not using an listener to detect button press. 问题是您没有使用侦听器来检测按钮的按下情况。 It is not advisable to change the button text use a text view instead!
建议不要使用文本视图来更改按钮文本!
Most likely he has set onClick
attribute in the xml, although only he'll be able to clarify that. 他很可能已经在xml中设置了
onClick
属性,尽管只有他才能澄清这一点。
I'm saying that because I see View as a parameter in the method st(View v)
. 我说这是因为我在方法
st(View v)
中将View视为参数。
Although if the attribute onClick is not set in xml @Audi 's solution would be good. 尽管如果未在xml中设置onClick属性,则@Audi的解决方案会很好。 Though on the contrary if the attribute has been set in the xml then you should check if
view.getId() == R.id.button
then apply a button cast to your view and set its text to "Stopped". 相反,如果已在xml中设置了属性,则应检查
view.getId() == R.id.button
然后将按钮view.getId() == R.id.button
应用到视图并将其文本设置为“ Stopped”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.