转/翻转的两个布局与按一下按钮 [英] Turn/flip for the two layouts with button click

查看:148
本文介绍了转/翻转的两个布局与按一下按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个布局(XML文件),我想从翻转一个网页到另一个,这两个XML文件是main.xml中和register.xml,如果我main.xml中的页面应该打开并显示点击登入按钮register.xml以及在register.xml如果我点击提交按钮,它应该转向main.xml中我尝试了很多的活动,我coldnt做到这一点,因为我是新到Android

请别人分享code对我来说,

这是我的两个XML codeS

main.xml中

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直
    机器人:重力=CENTER_HORIZONTAL
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:背景=@绘制/背景>    <的LinearLayout
    机器人:方向=横向
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_marginTop =10dp机器人:背景=@绘制/ loginapp>
 <按钮的android:layout_width =49px机器人:layout_height =44px机器人:背景=@绘制/ login_home_btn_over_green机器人:ID =@ + ID / widget38>< /按钮>
    < / LinearLayout中>    < RelativeLayout的机器人:ID =@ + ID / relativeLayout1机器人:比重=中心机器人:layout_marginTop =25dip机器人:layout_height =177dip机器人:背景=@绘制/ login_form_bg_green的android:layout_width = 296dip>
     < EditText上的android:layout_marginRight =0dip机器人:ID =@ + ID / userNameBox的android:layout_width =200像素机器人:背景=@机器人:可绘制/ editbox_background机器人:MAXLINES =1机器人:layout_marginLeft =85dip的android:inputType下=文本的android:layout_height =WRAP_CONTENT>< /&的EditText GT;
     < EditText上的android:layout_marginRight =0dip机器人:ID =@ + ID / passwordBox的android:layout_width =200像素机器人:背景=@机器人:可绘制/ editbox_background机器人:MAXLINES =1机器人:layout_marginTop =45dip机器人:layout_marginLeft =85dip的android:inputType下=TEXT | textVisiblePassword机器人:layout_height =WRAP_CONTENT>< /&的EditText GT;
    < / RelativeLayout的>    <的LinearLayout机器人:ID =@ + ID / ll_three的android:layout_width =FILL_PARENT机器人:layout_height =WRAP_CONTENT机器人:方向=横向机器人:layout_below =@ + ID / ll_two机器人:比重= 中心>
      <按钮
        机器人:文字=登录
        机器人:ID =@ + ID / Button01
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_marginTop =5dip
        机器人:layout_marginRight =15dip
        机器人:layout_height =WRAP_CONTENT/>
    <按钮
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_marginTop =5dip
        机器人:ID =@ + ID / Button02
        机器人:文字=新用户/>
 < / LinearLayout中>   < / LinearLayout中>

register.xml是

 <?XML版本=1.0编码=UTF-8&GT?;
< TableLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:stretchColumns =0>    <&的TableRow GT;
     < TextView的机器人:ID =@ + ID / TextView的
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文字=寄存器:/>
    < /&的TableRow GT;    <&的TableRow GT;
        < TextView的机器人:ID =@ + ID / TextView01
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文字=名字:/>
    < EditText上的android:layout_marginRight =0dip
      机器人:ID =@ + ID / userNameBox机器人:背景=@机器人:可绘制/ editbox_background
        机器人:MAXLINES =1机器人:layout_marginLeft =15dip机器人:layout_weight =1
        安卓的inputType =文本的android:layout_height =35px的android:layout_width =0dip>< /&的EditText GT;
    < /&的TableRow GT;    <&的TableRow GT;
    < TextView的机器人:ID =@ + ID / TextView02
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文字=姓氏:/>
    < EditText上的android:layout_marginRight =0dip
      机器人:ID =@ + ID / userNameBox的android:layout_width =200像素机器人:layout_weight =1
      机器人:背景=@机器人:可绘制/ editbox_background机器人:MAXLINES =1
       机器人:layout_marginLeft =15dip的android:inputType下=TEXT
       机器人:layout_height =35px>< /&的EditText GT;
    < /&的TableRow GT;
    <&的TableRow GT;
        < TextView的机器人:ID =@ + ID / TextView03
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文字=电子邮件:/>
    <的EditText
     机器人:ID =@ + ID / userNameBox的android:layout_width =200像素
      机器人:背景=@机器人:可绘制/ editbox_background机器人:layout_weight =1
       机器人:MAXLINES =1机器人:layout_marginLeft =15dip机器人:layout_marginRight =0dip
       安卓的inputType =文本的android:layout_height =35px>
       < /&的EditText GT;
    < /&的TableRow GT;    <&的TableRow GT;
     < TextView的机器人:ID =@ + ID / TextView03
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文字=手机号码:/>
    < EditText上的android:layout_marginRight =0dip
     机器人:ID =@ + ID / userNameBox的android:layout_width =200像素机器人:layout_weight =1
      机器人:背景=@机器人:可绘制/ editbox_background
       机器人:MAXLINES =1机器人:layout_marginLeft =15dip
        安卓的inputType =文本的android:layout_height =35px>
        < /&的EditText GT;    < /&的TableRow GT;  <&的TableRow GT;
  <按钮
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=注册
        机器人:ID =@ + ID / Button01
          机器人:填充=3dip
          机器人:layout_marginLeft =45dip
          机器人:layout_marginRight =90dip
          机器人:layout_marginTop =15dip
          机器人:layout_column =1          机器人:layout_width =FILL_PARENT
          机器人:layout_weight =1/>
  < /&的TableRow GT;
< / TableLayout>

这是我的Java文件

login.java

 包com.android;
进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.Button;公共类扩展登录活动{
    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);        按钮旁边=(按钮)findViewById(R.id.Button02);
        next.setOnClickListener(新View.OnClickListener(){
            公共无效的onClick(查看视图){
                意图myIntent =新意图(view.getContext(),Register.class);
                startActivityForResult(myIntent,0);
            }        });
    }
}

Register.java

 包com.android;进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.Button;公共类注册扩展活动{    / **当第一次创建活动调用。 * /
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.register);        按钮旁边=(按钮)findViewById(R.id.Button01);
        next.setOnClickListener(新View.OnClickListener(){
            公共无效的onClick(查看视图){
                意向意图=新的Intent();
                的setResult(RESULT_OK,意向);
                完();
            }        });
    }}


解决方案

使用活动的意图和

您将要创建两个活动的。一个是你的主,为您的登录屏幕。每次这个活动的的得到一个布局(如你在XML中定义它们)。

要打开一个新的活动,或回到另一个活动,意图< /一>被使用。我创建了如何做到这一点的教程,可以发现的这里

有关code什么

好吧,首要的事情:在你的XML布局定义,你可以使用'的的onClick '-Attribute定义被称为一个方法的名称,当您的按钮被点击(如显示在上面的教程)。

此外,如果在你的code发生任何错误,你不知道那里的错误就在于,你可以使用机器人的日志机制找到它。看到在Eclipse日志输出,你需要打开一个新的观点:的窗口 - >显示视图 - >其他... - > Android的 - > LogCat中的。你应该得到一个例外,在这里发表的异常输出。

I have two layouts(xml files) and I want to flip from one page to another, the two xml files are main.xml and register.xml, if I click signin button in main.xml the page should turn and show register.xml and also in register.xml if I click the submit button it should turn to main.xml I tried a lot with the activity and i coldnt do it as I am new to android

please someone share the code for me,

here is my two xml codes

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 is

<?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>

HERE ARE MY JAVA FILES

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

        });
    }}

解决方案

Using Activity's and Intents

You will want to create two Activity's. One for your Main, on for your Sign-in Screen. Each of this Activity's gets a Layout (like you defined them in XML).

To open a new Activity or get back to another Activity, Intents are used. I created a tutorial on how to do this, it can be found here.

What about your code

Okay, first things first: In your XML Layout-definition, you can use the 'onClick'-Attribute to define a method name which is called, when your button gets clicked (like shown in the Tutorial above).

Also, if any errors occurs in your code and you have no idea where the error lies, you can use Androids Logging mechanism to find it out. To see the Log-Output in Eclipse, you need to open a new view: Window -> Show View -> Other... -> Android -> LogCat. You should get an Exception, post the Exception-Output here.

这篇关于转/翻转的两个布局与按一下按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆