Onclick文本如何重定向到android中的另一个XML页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21327379/
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
Onclick text how can I redirect to another XML page in android?
提问by Nehil Mistry
I am new to android. Can I get to know Onclick text how can I redirect to another XML page in Android ?
我是安卓新手。我可以了解 Onclick 文本如何重定向到 Android 中的另一个 XML 页面?
回答by SMK
You can redirect to another xml through a button click, But here I think you have asked how to do it clicking on some text, for a example if you want to click on this text "click to sign up" and redirect you to the signup page.
您可以通过单击按钮重定向到另一个 xml,但在这里我想您已经问过如何单击某些文本来执行此操作,例如,如果您想单击此文本“单击以注册”并将您重定向到注册页。
So here is how I did it.
所以这就是我是如何做到的。
1)for text click
1)对于文本点击
Inside Main Activity class you write the following code.
在 Main Activity 类中,您编写以下代码。
//Text redirect. signup textview should be created within mainActivity.xml
final TextView signup=(TextView)findViewById(R.id.signup);
signup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v1) {
Intent launchActivity1= new Intent(MainActivity.this,SignUp.class);
startActivity(launchActivity1);
}
});
In the AndroidManifest file, You create a New Activity
在 AndroidManifest 文件中,您创建一个新活动
<!-- Sign Up activity -->
<activity
android:name=".SignUp"
android:label="@string/app_name">
<intent-filter>
<action android:name="com.example.xxxx.SignUp"/>
<categeory android:name="android.intent.categeory.LAUNCHER"/>
</intent-filter>
</activity>
Sign Up Java class.You make signup.xml appear inside this class by accessing it through the R.id.layout
注册 Java 课程。您可以通过 R.id.layout 访问它使 signup.xml 出现在此类中
package com.example.xxxx;
import android.app.Activity;
import android.os.Bundle;
public class SignUp extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sign_up);
}
}
.
.
2) for button click
2)对于按钮点击
You will do the same as for Text click.
您将执行与文本单击相同的操作。
In MainActivity.java class
在 MainActivity.java 类中
final Button button=(Button)findViewById(R.id.loginbtn);
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent launchactivity= new Intent(MainActivity.this,Details.class);
startActivity(launchactivity);
Then you will create A new Activity in AndroidManifest. You Have to have a Details.java class. Since you have mentioned to redirect to Details.class, according to this example. Inside Details class like in previous access the relevant xml file.
然后你将在 AndroidManifest 中创建一个新的 Activity。您必须有一个 Details.java 类。既然你已经提到重定向到 Details.class,根据这个例子。在 Details 类中,就像之前访问相关的 xml 文件一样。
"click here to signup" in the following image will redirect you to the signup.xml .
下图中的“单击此处注册”会将您重定向到 signup.xml 。


回答by hcpl
I'm guessing here that with 'new XML page' you refer to another view?
我在这里猜测“新 XML 页面”是指另一个视图?
Most basic way to accomplish this is to make 2 activities with each a different layout (that XML file). On activity 1 add a button. In onclicklistener for that button start the other activity.
完成此操作的最基本方法是使用不同的布局(即 XML 文件)制作 2 个活动。在活动 1 上添加一个按钮。在该按钮的 onclicklistener 中启动其他活动。
Use the visual tools in your IDE to create the activities. Use below code to switch to other activity:
使用 IDE 中的可视化工具创建活动。使用以下代码切换到其他活动:
public void startActivity2(View view) {
Intent intent = new Intent(this, Activity2.class);
startActivity(intent);
}
More info at http://developer.android.com/training/basics/firstapp/starting-activity.html
更多信息请访问http://developer.android.com/training/basics/firstapp/starting-activity.html
回答by Adam Radomski
There so many ways to do it i can't even describe them all :) Possibly you thing about starting new activity. So you need to create new activity class with proper layout (in xml) and then start it with intent. Heres an example
有很多方法可以做到,我什至无法全部描述:) 可能是您开始新活动的事情。因此,您需要使用适当的布局(在 xml 中)创建新的活动类,然后以意图启动它。 这是一个例子

