通过单击按钮旋转/翻转两种布局

2024-04-08

我有两个布局(xml 文件),我想从一个页面翻转到另一个页面,这两个 xml 文件是 main.xml 和 register.xml,如果我单击 main.xml 中的登录按钮,页面应该翻转并显示 register.xml并且在 register.xml 中,如果我单击提交按钮,它应该转向 main.xml 我在该活动中尝试了很多,但我冷不这样做,因为我是 Android 新手

请有人帮我分享代码,

这是我的两个 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>

注册.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 文件

登录.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);
            }

        });
    }
}

注册.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();
            }

        });
    }}

使用活动和意图

您将需要创建两个活动。一个用于您的主屏幕,一个用于您的登录屏幕。每个 Activity 都有一个布局(就像您在 XML 中定义的那样)。

要打开新活动或返回另一个活动,Intents http://developer.android.com/guide/topics/intents/intents-filters.html被使用。我创建了一个关于如何执行此操作的教程,可以找到here http://www.dreamincode.net/forums/topic/223914-activities-intents-and-something-on-events/.

你的代码怎么样

好的,首先要做的事情是:在 XML 布局定义中,您可以使用 'onClick http://developer.android.com/reference/android/widget/Button.html'-属性定义单击按钮时调用的方法名称(如上面的教程所示)。

另外,如果您的代码中出现任何错误,并且您不知道错误出在哪里,您可以使用 Android日志记录机制 http://developer.android.com/reference/android/util/Log.html找出来。要在 Eclipse 中查看日志输出,您需要打开一个新视图:窗口 -> 显示视图 -> 其他... -> Android -> LogCat。您应该得到一个异常,在此处发布异常输出。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

通过单击按钮旋转/翻转两种布局 的相关文章

随机推荐