如何在 android 上从一个页面导航到另一个页面?(共 3 页)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10036157/
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 page to another on android?(Total:3 pages)
提问by Μην?? Γκ?ργε?
I am new to Android development and I have a question regarding how to navigate from one page to another. Actually,what I want to do is this: Open app,first page appears,it says "Hello I am activity 1".Then there is a button which says "Next",you press next then you navigate to second page where it says "Hello I am activity 2".In this page there are two buttons,first says "Previous" which takes you back to page 1 and second says "Next" which takes you to page 3. Basically,this is where I am stuck,page 1 and 2 works fine,both next and previous buttons but I can't navigate to 3rd page when I press the "next" button from page 2. I have uploaded my source code here so you guys can download it and import it to Eclipse in order to see exactly what I have done.
我是 Android 开发的新手,我有一个关于如何从一个页面导航到另一个页面的问题。实际上,我想要做的是:打开应用程序,出现第一页,上面写着“你好,我是活动 1”。然后有一个按钮,上面写着“下一步”,你按下一步,然后导航到第二页,上面写着“你好,我是活动 2”。在这个页面有两个按钮,第一个是“上一个”,它会带你回到第 1 页,第二个是“下一个”,它会带你到第 3 页。基本上,这就是我被卡住的地方,第 1 页和第 2 页工作正常,下一个和上一个按钮都可以,但是当我按第 2 页的“下一个”按钮时,我无法导航到第 3 页。我已经在这里上传了我的源代码,所以你们可以下载它并将其导入到Eclipse 以便确切地看到我做了什么。
Click hereto download.
点击这里下载。
Would be glad if someone could help mates, Thanks in advance.
如果有人可以帮助队友,我会很高兴,提前致谢。
OK,code is shown here,I have created 3 activities which I also registered at Manifest and I also have created 3 layouts for these 3 activities.
好的,代码显示在这里,我创建了 3 个活动,我也在 Manifest 注册了这些活动,我还为这 3 个活动创建了 3 个布局。
Activity 1
活动一
import android.app.Activity;<br>
import android.content.Intent;<br>
import android.os.Bundle;<br>
import android.view.View;<br>
import android.widget.Button;<br>
public class Activity1 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button next = (Button) findViewById(R.id.Button01);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Activity2.class);
startActivityForResult(myIntent, 0);
}
});
}
}
Activity2
活动2
import android.app.Activity;<br>
import android.content.Intent;<br>
import android.os.Bundle;<br>
import android.view.View;<br>
import android.widget.Button;<br>
public class Activity2 extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
Button next = (Button) findViewById(R.id.Button02);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
}
public void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
Button next = (Button) findViewById(R.id.Button04);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent1 = new Intent(view.getContext(), Activity3.class);
startActivityForResult(myIntent1, 0);
}
});
}
}
Activity3
活动3
import android.app.Activity;<br>
import android.content.Intent;<br>
import android.os.Bundle;<br>
import android.view.View;<br>
import android.widget.Button;<br>
public class Activity3 extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main3);
Button next = (Button) findViewById(R.id.Button04);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
}
}
main.xml
主文件
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="This is Activity 1" />
<Button android:text="Next"
android:id="@+id/Button01"
android:layout_width="250px"
android:textSize="18px"
android:layout_height="55px">
</Button>
</LinearLayout>
main2.xml
main2.xml
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="This is Activity 2" />
<Button android:text="Previous"
android:id="@+id/Button02"
android:layout_width="250px"
android:textSize="18px"
android:layout_height="55px">
</Button>
<Button
android:layout_width="162dp"
android:layout_height="34dp"
android:text="Next"
android:id="@+id/Button04"
android:textSize="18px" />
</LinearLayout>
main3.xml
main3.xml
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="This is Activity 3" />
I have also registered my activities in Manifest.
我也在 Manifest 中注册了我的活动。
回答by CommonsWare
Step #1: Replace startActivityForResult()
with startActivity()
in Activity1
第 1 步:在 Activity1 中替换startActivityForResult()
为startActivity()
Step #2: Remove your current code in the onClick()
from Activity2 and replace it with a call to startActivity()
to start Activity3
第 2 步:onClick()
从 Activity2 中删除当前代码并将其替换为调用startActivity()
以启动 Activity3
Step #3: Completely rewrite Activity3, as either it will not compile or it will crash at runtime, as you are referring to a widget that does not exist (Button04)
第 3 步:完全重写 Activity3,因为它要么不会编译,要么会在运行时崩溃,因为您指的是不存在的小部件 (Button04)
回答by Zer0
I'm not sure if it't too late but if you still need a clear answer then:
我不确定现在是否为时已晚,但如果您仍然需要明确的答案,那么:
You will need to start activities
. There isn't
a DIRECT page navigation code
, but instead there is a start Activity
. By starting an activity
you are able to go to another page because the page is related to the activity.
你将需要start activities
。有isn't
一个DIRECT page navigation code
,但有一个start Activity
。通过starting an activity
你能够去到另一个页面,因为页面是相关的活动。
Intent myIntent = new Intent(Enter_Your_Current_Activity.this, Enter_The_Activity_You_Want_To_Navigate_To.class);
startActivity(myIntent);
Hope this helps!
希望这可以帮助!
p.s What I use in my code.
ps 我在我的代码中使用了什么。
回答by ilcc chalakudy
a simple way to navigate one page to another is startActivity new Intent(firstpagename.this,secondpagename.class);
将一个页面导航到另一个页面的简单方法是 startActivity new Intent(firstpagename.this,secondpagename.class);
回答by sivaurbozz
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Activity1 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button next = (Button) findViewById(R.id.Button01);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Activity2.class);
startActivityForResult(myIntent, 0);
}
});
}
}