gpt4 book ai didi

android - Android中通过Intent传递数据,只想要点击次数

转载 作者:行者123 更新时间:2023-11-30 04:21:37 25 4
gpt4 key购买 nike

我有一个程序从一个 Activity 开始到下一个 Activity ,他们是点击按钮,在一定时间后它返回到起始页面并报告点击次数。

这是我的代码:点击次数是第一个 Activity

public class ClickCountActivity extends Activity {
/** Called when the activity is first created. */
Button next;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
next=(Button) findViewById(R.id.NextButton);
//---------------------------------------------------------------
next.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
Intent i = new Intent(ClickCountActivity.this, startClickActivity.class);
i.putExtra("comingFrom", "come");
final int result=1;
startActivityForResult(i,result);
}
});
//---------------------------------------------------------------------------
}
}

public class startClickActivity extends Activity {
/** Called when the activity is first created. */

Button clicker;
int counter ;
Timer timer = new Timer(); // use timer to start a new task
MyTimerTask task = new MyTimerTask();
final long seconds = 3;

Intent p = getIntent();
String answer = p.getStringExtra("comingFrom");

class MyTimerTask extends TimerTask {
public void run()
//override run method
{

Intent x = new Intent(startClickActivity.this, ClickCountActivity.class);
x.putExtra("returnStr", answer);
setResult(RESULT_OK,x);
startActivity(x);

}
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.action);

clicker=(Button) findViewById(R.id.Clicker);

//---------------------------------------------------------------
clicker.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
counter++; // counts number of clicks
task.cancel(); // cancels current task
task = new MyTimerTask(); //create new task
timer.schedule(task,seconds*1000L); // start a new timer task in 5seconds (timertask, seconds(long))
// System.out.println(counter);

}
});

}
}

最佳答案

Your code in run method should be this:

Intent x = new Intent(startClickActivity.this, ClickCountActivity.class);
x.putExtra("returnStr", counter);
setResult(RESULT_OK,x);
finish();

您需要通过号码。计数,即 Intent 计数器,并从 ClickCountActivity 类的 onActivityResult(int requestCode, int resultCode, Intent data) 方法收集它。该值在数据 Intent 中传递,可以使用 int counterValue = data.getIntegerExtra("returnStr", 0);

查询

关于android - Android中通过Intent传递数据,只想要点击次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9170385/

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