android - 如何在用户可以导航回活动的时间间隔后切换活动?
问题描述
我有两个活动;一个是IntermediateActivity
,一个是ButtonPress
活动。有IntermediateActivity
一个 5 秒计时器。计时器结束后,ButtonPress
活动打开,其中用户必须在一段时间内(现在为 2 秒)按下放置在屏幕上随机位置的按钮,然后我开始一个意图,IntermediateActivity
如果用户可以按下按钮或时间用完。
正在发生的事情是,对于 的运行ButtonPress
,IntermediateActivity
它会自行打开,有时,计时器上的计时器IntermediateActivity
未完成,但ButtonPress
活动已启动。添加计时器ButtonPress
导致这两个活动的行为非常不稳定。
以下是我根据计时器和用户操作IntermediateActivity
在Activity 中调用意图的方式:ButtonPress
public class ButtonPress extends AppCompatActivity implements View.OnClickListener {
private Button button;
private DisplayMetrics displaymetrics;
private RelativeLayout loOPenArea;
private Handler mHandler = new Handler();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.button_press);
button = findViewById(R.id.my_button);
button.setClickable(true);
button.setOnClickListener(this);
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
Intent i = new Intent(ButtonPress.this, IntermediateActivity.class);
startActivity(i);
finish();
}
}, 2000);
// Wait until the first layout to get button size and placement to compute x/y placement range.
button.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
button.getViewTreeObserver().removeOnGlobalLayoutListener(this);
// Capture width and x/y placement ranges here
final Button button = (Button) findViewById(R.id.my_button);
displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
loOPenArea = (RelativeLayout) findViewById(R.id.open_area_press);
// Button will have a horizontal margin from zero to the width of its parent less
// the width of the button less the initial left side of the button which is likely
// to be the initial margin.
int buttonMaxXMargin = loOPenArea.getWidth() - button.getWidth() - button.getLeft();
// Similar for the vertical placement.
int buttonMaxYMargin = loOPenArea.getHeight() - button.getHeight() - button.getTop();
int leftMargin = new Random().nextInt(buttonMaxXMargin);
int topMargin = new Random().nextInt(buttonMaxYMargin);
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) button.getLayoutParams();
p.setMargins(leftMargin, topMargin, 0, 0);
button.setLayoutParams(p);
}
});
}
@Override
public void onClick(View v) {
Intent i = null;
switch (v.getId()) {
case R.id.my_button:
i = new Intent(ButtonPress.this, IntermediateActivity.class);
startActivity(i);
finish();
break;
}
}
}
我尝试了多种方法,例如使用该finish()
函数并android:finishOnTaskLaunch="true"
在清单文件中添加这一行,但它仍然无法正常工作。
解决方案
推荐阅读
- ruby-on-rails - 如何在 will_paginate Rails 中将数字变为点
- android - 焦点在 EditText 上时 ViewPager2 片段内容消失
- flutter - 如何为可选参数使用非常量值
- javascript - 有没有办法在一次调用中使用一组用户 ID 从 Keycloak API 查询用户信息?
- html - 如何更改带有type = time的html输入标签附带的小时钟图标的颜色
- python - 无法在 Pandas DataFrames 中腌制本地对象“Styler”
- java - 在 Java 中,变量是否以其透视值存储在堆栈或堆中?
- python - Python中内存泄漏的单元测试
- java - 如何改进我的编码器/解码器?我的加密方法合适吗?
- node.js - 当我使用 ip 地址访问我的网站时,如何让浏览器显示域名?