Java 如何从一个屏幕导航到另一个屏幕
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1102050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to navigate from one screen to another screen
提问by Kumar
How to navigate from one Activity screen to another Activity screen? In the first screen I'm having one button if I click the button it has to move to another Activity screen.
如何从一个活动屏幕导航到另一个活动屏幕?在第一个屏幕中,我有一个按钮,如果我单击该按钮,它必须移动到另一个活动屏幕。
采纳答案by Chiwai Chan
OnClickListener onClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(action));
}
};
Button button = (Button) findViewById(id);
button.setOnClickListener(onClickListener);
回答by yanchenko
The most trivial case (called from activity):
最简单的情况(从活动中调用):
startActivity(new Intent(this, ActivityToLaunch.class));
More details here: http://developer.android.com/guide/topics/fundamentals.html
更多细节在这里:http: //developer.android.com/guide/topics/fundamentals.html
回答by Will
Button x.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
Intent i = new Intent(y.this, Activity.class);
startActivity(i);
}
});
Here we've defined a listener for Button x. The OS will call this method and start the Activity referenced in Intent i.
这里我们为 Button x 定义了一个监听器。操作系统将调用此方法并启动 Intent i 中引用的 Activity。
Here's the official tutorial example: http://developer.android.com/guide/tutorials/notepad/notepad-ex2.html
这是官方教程示例:http: //developer.android.com/guide/tutorials/notepad/notepad-ex2.html
回答by Azhar
final Context cont = this;
Button btnClickABC =(Button)findViewById(R.id.btnClickABC);
btnClickABC.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(cont, NextActivity.class));
}
});
回答by Mostafiz
startActivity(new Intent(this,newActivity.class));
回答by kusuma
public void onClick(View v)
{
Intent myintent = new Intent(currentclass.this, nextactivity.class);
startActivity(myintent);
}
回答by Umesh
Use following code..I hope this will help you.
使用以下代码..我希望这会帮助你。
Button button = (Button)findViewById(R.id.xxx);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(CurrentActivity.this,NextActivity.class);
startActivity(intent);
}
});
xxxis id from your xml of your Button.
xxx是来自您 Button 的 xml 的 ID。
回答by G.S. Shekhawat
Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(TestActivity.this,second.class));
}
});
回答by Nikhil Agrawal
This task can be accomplished using one of the android's main building block named as Intents and One of the methods public void startActivity (Intent intent)
which belongs to your Activity class.
可以使用名为 Intents 的 android 主要构建块之一和public void startActivity (Intent intent)
属于您的 Activity 类的方法之一来完成此任务。
An intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver components, and startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a background Service.
意图是对要执行的操作的抽象描述。它可以与 startActivity 一起使用以启动 Activity,broadcastIntent 将其发送到任何感兴趣的 BroadcastReceiver 组件,以及 startService(Intent) 或 bindService(Intent, ServiceConnection, int) 与后台服务通信。
An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.
Intent 提供了一种在不同应用程序中的代码之间执行后期运行时绑定的工具。它最重要的用途是启动活动,可以将其视为活动之间的粘合剂。它基本上是一个被动数据结构,包含要执行的操作的抽象描述。
Refer the official docs -- http://developer.android.com/reference/android/content/Intent.html
参考官方文档——http: //developer.android.com/reference/android/content/Intent.html
public void startActivity (Intent intent)
-- Used to launch a new activity.
public void startActivity (Intent intent)
-- 用于启动新活动。
So suppose you have two Activity class and on a button click's OnClickListener()
you wanna move from one Activity to another then --
因此,假设您有两个 Activity 类,并且在单击按钮时,OnClickListener()
您想从一个 Activity 移动到另一个 Activity,然后 -
PresentActivity-- This is your current activity from which you want to go the second activity.
NextActivity-- This is your next Activity on which you want to move.
PresentActivity-- 这是您要从中进行第二个活动的当前活动。
NextActivity-- 这是您要移动的下一个活动。
So the Intent would be like this
所以意图会是这样的
Intent(PresentActivity.this, NextActivity.class)
Finally this will be the complete code
最后这将是完整的代码
public class PresentActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.content_layout_id);
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
Intent activityChangeIntent = new Intent(PresentActivity.this, NextActivity.class);
// currentContext.startActivity(activityChangeIntent);
PresentActivity.this.startActivity(activityChangeIntent);
}
});
}
}
This exmple is related to button click you can use the code anywhere which is written inside button click's OnClickListener()
at any place where you want to switch between your activities.
这个例子与按钮点击有关,您可以在按钮点击内的OnClickListener()
任何地方使用代码,在您想要在活动之间切换的任何地方。
回答by AyAz
Switching from one activity to another is really simple, but tricky for a new one.
Your next class must be defined in AndroidManifest.xml
. This is tester class:
从一项活动切换到另一项活动非常简单,但对于新活动来说却很棘手。您的下一个类必须在AndroidManifest.xml
. 这是测试人员类:
<activity
android:name=".Tester"
android:label="@string/title_activity_tester" >`enter code here`
</activity>
final Button button = (Button) findViewById(R.id.btnGo);// btnGo is id
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(CurrentClass.this, Tester.class);
startActivity(i);
}