如何在同一活动中单击两个不同的按钮导航到两个不同的活动?

时间:2014-02-13 04:44:40

标签: android

我开发了三项活动。我想通过分别从main_activity到regfrm活动和登录活动点击两个不同的按钮(注册按钮和登录按钮)来导航。我的问题是,当我运行我的项目时,单击按钮注册到regfrm活动时,它不会导航。

main activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/pizza1"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="90dp"
        android:text="REGISTER" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:layout_marginLeft="54dp"
        android:layout_toRightOf="@+id/button1"
        android:text="LOGIN" />

</RelativeLayout>

主要活动java代码

package com.example.pizza;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        addButtonClickEventListener();
    }

        public void addButtonClickEventListener() {
        // TODO Auto-generated method stub
        Button button =(Button)findViewById(R.id.button1);
        Button button1 =(Button)findViewById(R.id.button2);
        final Context context = MainActivity.this;
        button1.setOnClickListener(new OnClickListener() 
        {
            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                Intent intent = new Intent (context,RegActivity.class);
                Intent intent1 = new Intent (context,loginActivity.class);
                startActivity(intent);
                startActivity(intent1);
            }   

        });
        }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

// regfrm acivity

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="@drawable/view"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".RegActivity" >
      <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button4"
        android:layout_alignLeft="@+id/textView5"
        android:text="SUBMIT" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView3"
        android:layout_alignBottom="@+id/textView3"
        android:layout_alignRight="@+id/editText1"
        android:ems="10"
        android:inputType="textPassword" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView3"
        android:layout_alignParentRight="true"
        android:ems="10"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/editText3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView4"
        android:layout_alignBottom="@+id/textView4"
        android:layout_alignParentRight="true"
        android:ems="10"
        android:inputType="textEmailAddress" />

    <EditText
        android:id="@+id/editText4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/textView5"
        android:ems="10"
        android:inputType="textPostalAddress" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/editText4"
        android:layout_below="@+id/editText4"
        android:layout_marginTop="80dp"
        android:text="CANCEL" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="26dp"
        android:text="PASSWORD"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="22dp"
        android:text="REGISTRATION FORM"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView2"
        android:text="USER NAME"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView4"
        android:layout_marginTop="58dp"
        android:text="ADDRESS"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textView5"
        android:layout_below="@+id/editText2"
        android:layout_marginTop="48dp"
        android:text="E-MAIL ID"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

//注册活动java代码

package com.example.pizza;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class RegActivity extends Activity 
{
    @Override
    protected void onCreate(Bundle savedInstancestate)
    {
        super.onCreate(savedInstancestate);
        setContentView(R.layout.regfrm);
        addButtonOnClickEventListener();

    }

    private void addButtonOnClickEventListener() 
    {
        // TODO Auto-generated method stub
        Button button =(Button)findViewById(R.id.button3);
        final Context context = RegActivity.this;
        button.setOnClickListener(new OnClickListener() 
        {
            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                Intent intent = new Intent (context,WelActivity.class);
                startActivity(intent);
            }   

        });
        }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
// login activity           
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="#ffff0000"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/butlogin"
        android:text="username"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/password"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="16dp"
        android:ems="10" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/username"
        android:layout_marginTop="16dp"
        android:text="password"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/butlogin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/password"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="61dp"
        android:text="Login" />

    <EditText
        android:id="@+id/password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView3"
        android:layout_marginRight="22dp"
        android:layout_marginTop="21dp"
        android:ems="10"
        android:inputType="textPassword" >

        <requestFocus />
    </EditText>

</RelativeLayout>

//登录活动java代码

package com.example.pizza;
import com.example.pizza.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class loginActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstancestate)
    {
        super.onCreate(savedInstancestate);
        setContentView(R.layout.login);
        addButtonOnClickEventListener();

    }

    private void addButtonOnClickEventListener() {
        // TODO Auto-generated method stub




        Button button =(Button)findViewById(R.id.butlogin);
        final Context context = loginActivity.this;
        button.setOnClickListener(new OnClickListener() 
        {
            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                Intent intent = new Intent (context,WelActivity.class);
                startActivity(intent);
            }   

        });
        }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

// manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.pizza"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.pizza.MainActivity"
            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="com.example.pizza.RegActivity"></activity>
        <activity android:name="com.example.pizza.loginActivity"></activity>
        <activity android:name="com.example.pizza.WelActivity"></activity>
        <activity android:name="com.example.pizza.OrderActivity"></activity>
        <activity android:name="com.example.pizza.MenuActivity"></activity>
        <activity android:name="com.example.pizza.BillActivity"></activity>
        <activity android:name="com.example.pizza.ThankuActivity"></activity>




         </application>


</manifest>

logcat错误:

02-13 15:24:05.520: D/dalvikvm(885): GC_FOR_ALLOC freed 44K, 4% free 3164K/3276K, paused 200ms, total 252ms
02-13 15:24:06.370: D/gralloc_goldfish(885): Emulator without GPU emulation detected.
02-13 15:24:22.080: I/Choreographer(885): Skipped 80 frames!  The application may be doing too much work on its main thread.
02-13 15:24:37.430: I/Choreographer(885): Skipped 30 frames!  The application may be doing too much work on its main thread.
02-13 15:24:43.170: I/Choreographer(885): Skipped 33 frames!  The application may be doing too much work on its main thread.
02-13 15:24:43.950: D/dalvikvm(885): GC_FOR_ALLOC freed 40K, 4% free 3369K/3476K, paused 80ms, total 84ms
02-13 15:24:43.960: I/dalvikvm-heap(885): Grow heap (frag case) to 3.879MB for 552016-byte allocation
02-13 15:24:44.070: D/dalvikvm(885): GC_FOR_ALLOC freed 1K, 3% free 3906K/4016K, paused 102ms, total 102ms
02-13 15:24:44.450: I/Choreographer(885): Skipped 32 frames!  The application may be doing too much work on its main thread.
02-13 15:24:45.120: I/Choreographer(885): Skipped 92 frames!  The application may be doing too much work on its main thread.
02-12 23:22:47.564: D/dalvikvm(885): GC_FOR_ALLOC freed 28K, 3% free 3966K/4064K, paused 100ms, total 107ms
02-12 23:22:47.594: I/dalvikvm-heap(885): Grow heap (frag case) to 4.675MB for 775012-byte allocation
02-12 23:22:47.734: D/dalvikvm(885): GC_FOR_ALLOC freed 1K, 3% free 4721K/4824K, paused 138ms, total 139ms
02-12 23:22:47.984: I/Choreographer(885): Skipped 693 frames!  The application may be doing too much work on its main thread.
02-12 23:22:49.394: W/ViewRootImpl(885): Dropping event due to no window focus: KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_BACK, scanCode=158, metaState=0, flags=0x8, repeatCount=0, eventTime=217187, downTime=217187, deviceId=0, source=0x101 }
02-12 23:22:49.554: I/Choreographer(885): Skipped 38 frames!  The application may be doing too much work on its main thread.
02-12 23:22:51.104: D/InputEventConsistencyVerifier(885): KeyEvent: ACTION_UP but key was not down.
02-12 23:22:51.104: D/InputEventConsistencyVerifier(885):   in com.android.internal.policy.impl.PhoneWindow$DecorView{b4e1e358 V.E..... R.....I. 0,0-480,800}
02-12 23:22:51.104: D/InputEventConsistencyVerifier(885):   0: sent at 217477000000, KeyEvent { action=ACTION_UP, keyCode=KEYCODE_BACK, scanCode=158, metaState=0, flags=0x8, repeatCount=0, eventTime=217477, downTime=217187, deviceId=0, source=0x101 }
02-12 23:22:55.714: I/Choreographer(885): Skipped 176 frames!  The application may be doing too much work on its main thread.
02-12 23:23:22.464: I/Choreographer(885): Skipped 187 frames!  The application may be doing too much work on its main thread.

1 个答案:

答案 0 :(得分:1)

您需要为这两个按钮设置点击侦听器

    Button button =(Button)findViewById(R.id.button1);
    Button button1 =(Button)findViewById(R.id.button2);
    final Context context = MainActivity.this;
    button.setOnClickListener(new OnClickListener() 
    {
        @Override
        public void onClick(View v)
        {
            // TODO Auto-generated method stub
            Intent intent = new Intent (context,RegActivity.class);
            startActivity(intent);

        }   

    });
    button1.setOnClickListener(new OnClickListener() 
    {
        @Override
        public void onClick(View v)
        {
            Intent intent1 = new Intent (context,loginActivity.class);
            startActivity(intent1);
        }   

    });