我有两个布局( xml文件),我想从一个页面翻到另一个,这两个xml文件是main.xml和register.xml,如果我点击main.xml中的登录按钮,页面应该会翻转并显示register.xml,如果我点击提交按钮,页面应该会变成main.xml。我已经尝试了很多,但我不能这样做,因为我对安卓是个新手。
有没有人帮我分享代码,
下面是我的两个xml代码
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background" >
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" android:background="@drawable/loginapp">
<Button android:layout_width="49px" android:layout_height="44px" android:background="@drawable/login_home_btn_over_green" android:id="@+id/widget38"></Button>
</LinearLayout>
<RelativeLayout android:id="@+id/relativeLayout1" android:gravity="center" android:layout_marginTop="25dip" android:layout_height="177dip" android:background="@drawable/login_form_bg_green" android:layout_width="296dip">
<EditText android:layout_marginRight="0dip" android:id="@+id/userNameBox" android:layout_width="200px" android:background="@android:drawable/editbox_background" android:maxLines="1" android:layout_marginLeft="85dip" android:inputType="text" android:layout_height="wrap_content"></EditText>
<EditText android:layout_marginRight="0dip" android:id="@+id/passwordBox" android:layout_width="200px" android:background="@android:drawable/editbox_background" android:maxLines="1" android:layout_marginTop="45dip" android:layout_marginLeft="85dip" android:inputType="text|textVisiblePassword" android:layout_height="wrap_content"></EditText>
</RelativeLayout>
<LinearLayout android:id="@+id/ll_three" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_below="@+id/ll_two" android:gravity="center">
<Button
android:text="Sign In"
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_marginTop="5dip"
android:layout_marginRight="15dip"
android:layout_height="wrap_content"/>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="5dip"
android:id="@+id/Button02"
android:text="New user"/>
</LinearLayout>
</LinearLayout>register.xml是
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="0">
<TableRow>
<TextView android:id="@+id/TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" REGISTER:"/>
</TableRow>
<TableRow>
<TextView android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Name:"/>
<EditText android:layout_marginRight="0dip"
android:id="@+id/userNameBox" android:background="@android:drawable/editbox_background"
android:maxLines="1" android:layout_marginLeft="15dip" android:layout_weight="1"
android:inputType="text" android:layout_height="35px" android:layout_width="0dip"></EditText>
</TableRow>
<TableRow>
<TextView android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Last Name:"/>
<EditText android:layout_marginRight="0dip"
android:id="@+id/userNameBox" android:layout_width="200px" android:layout_weight="1"
android:background="@android:drawable/editbox_background" android:maxLines="1"
android:layout_marginLeft="15dip" android:inputType="text"
android:layout_height="35px"></EditText>
</TableRow>
<TableRow>
<TextView android:id="@+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email:"/>
<EditText
android:id="@+id/userNameBox" android:layout_width="200px"
android:background="@android:drawable/editbox_background" android:layout_weight="1"
android:maxLines="1" android:layout_marginLeft="15dip" android:layout_marginRight="0dip"
android:inputType="text" android:layout_height="35px">
</EditText>
</TableRow>
<TableRow>
<TextView android:id="@+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mobile No:"/>
<EditText android:layout_marginRight="0dip"
android:id="@+id/userNameBox" android:layout_width="200px" android:layout_weight="1"
android:background="@android:drawable/editbox_background"
android:maxLines="1" android:layout_marginLeft="15dip"
android:inputType="text" android:layout_height="35px">
</EditText>
</TableRow>
<TableRow>
<Button
android:layout_height="wrap_content"
android:text="Register"
android:id="@+id/Button01"
android:padding="3dip"
android:layout_marginLeft="45dip"
android:layout_marginRight="90dip"
android:layout_marginTop="15dip"
android:layout_column="1"
android:layout_width="fill_parent"
android:layout_weight="1"/>
</TableRow>
</TableLayout>以下是我的JAVA文件
login.java
package com.android;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Login extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button next = (Button) findViewById(R.id.Button02);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Register.class);
startActivityForResult(myIntent, 0);
}
});
}
}Register.java
package com.android;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Register extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
Button next = (Button) findViewById(R.id.Button01);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
}}发布于 2011-04-23 19:42:49
使用Activity和Intents
您将需要创建两个活动,一个用于主活动,一个用于登录屏幕。每个活动都有一个布局(就像您在XML中定义的一样)。
要打开新的活动或返回到另一个活动,可以使用Intents。我创建了一个关于如何做到这一点的教程,它可以在here中找到。
你的代码呢?
好的,首先要做的是:在你的XML布局定义中,你可以使用‘onClick’属性来定义一个方法名,当你的按钮被点击时就会调用这个方法名(如上面的教程所示)。
此外,如果你的代码中出现了任何错误,而你不知道错误在哪里,你可以使用Androids Logging mechanism来找出它。要在Eclipse中查看日志输出,您需要打开一个新视图: Window -> Show View -> Other...->安卓-> LogCat。你应该会得到一个异常,在这里发布异常输出。
发布于 2011-06-13 22:46:16
你把register.class放到AndroidManifest.xml里了吗?应该看起来像这样
<application android:label="@string/app_name" android:icon="@drawable/icon">
<activity android:name=".login"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Register"></activity>
</application>发布于 2011-04-23 21:14:11
使用setContentView(R.Layout.main)在java代码中显示主布局。然后,将onClickListener设置为侦听用户单击"signin“以使用另一个setContentView(R.Layout.register)切换到其他视图。您可以在正在侦听的按钮的onClickListener用例中编写任何您喜欢的代码。
https://stackoverflow.com/questions/5763933
复制相似问题