开发者

Turn/flip for the two layouts with button click

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:backgr开发者_开发技巧ound="@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.


Have you put the register.class in the AndroidManifest.xml? Should look something like this

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


Use setContentView(R.Layout.main) to show the main layout in your java code. Then, set onClickListener to listen for the user to click "signin" to switch to other view using another setContentView(R.Layout.register). You can code whatever you like in the onClickListener cases for the buttons you are listening for.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜