gpt4 book ai didi

Android 在 Activity 之间切换

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

我想要做的是一个停留 5 秒并转到 Activity 1 的主屏幕。当我在 Activity 1 中单击一个按钮时,我会转到 Activity 2。我尝试了很多次单击该按钮但没有切换发生. 主屏幕(5 秒)=Main_Activity Activity1=selectpets.java Activity2=fishtank.java

onclick 监听器似乎是我不知道它有什么问题的问题

     Main Activity Code
package com.set.petshome;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
Button fButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Delay Code after 5 seconds
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
setContentView(R.layout.selectscreen); //where <next> is you target activity :)
}
}, 5000);
}
//Delay End
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

现在是 Selectpets 代码

package com.set.petshome;

import android.app.Activity;
import android.content.*;
import android.os.Bundle;
import android.view.*;
import android.widget.Button;
public class SelectPetsScreen extends Activity {
Button fButton;

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

//Button Fishtank Listener Start

fButton = (Button) findViewById(R.id.button1);

//Listening to button event
fButton.setOnClickListener(new View.OnClickListener() {

public void onClick(View arg0) {
//Starting a new Intent
Intent nextScreen = new Intent(getApplicationContext(), fishtank.class);
startActivity(nextScreen);

}
});
//Button Fishtank Listener End

}
}

鱼缸类代码

package com.set.petshome;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class fishtank extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ftank);



}

}

顺便说一句,应用程序没有错误,只是点击后没有切换
非常感谢

最佳答案

在这里你永远不会切换到下一个Activity,只是改变当前Activitylayout

 new Handler().postDelayed(new Runnable() {
@Override
public void run() {
setContentView(R.layout.selectscreen); //where <next> is you target activity :)
}
}, 5000);

您需要使用 Intent 而不是 setContentView()

Intent i = new Intent(MainActivity.this, SelectPetsScreen.this);
startActivity(i);

因为您实际上并没有进入下一个Activity(java 文件),所以您的onClick() 没有设置。

编辑

这就是你做的

public class MainActivity extends Activity {
Button fButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Delay Code after 5 seconds
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
setContentView(R.layout.selectscreen); //where <next> is you target activity :)
}
}, 5000);
}

这是你应该做的。注意 run() 函数的区别

public class MainActivity extends Activity {
Button fButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Delay Code after 5 seconds
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent i = new Intent(MainActivity.this, SelectPetsScreen.this);
startActivity(i);
}
}, 5000);
}

关于Android 在 Activity 之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18252645/

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