如何从Android中的一个活动启动活动 [英] How to launch activities from one activity in Android

查看:101
本文介绍了如何从Android中的一个活动启动活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好



我是Android开发的新手,我一直试图从另一个开展活动。让我们从主要活动说,我点击按钮启动家庭活动。然后从家庭活动,我希望能够根据点击的任何按钮启动5个不同的活动。我在网上尝试了很多资源,似乎没有一个对我有用。

从Main到Home它工作正常但从Home到其他活动,没有任何事情发生。



请协助。

PS。以下是我的示例代码:



MainActivity.java



包com.example.unisalabapp;



import android.os.Bundle;

import android.app.Activity;

导入android.content.Intent;

导入android.view.View;

导入android.view.View.OnClickListener;

import android.widget.Button;



公共类MainActivity扩展Activity实现OnClickListener {



Button btnLogin;



@Override

protected void onCreate(Bundle savedInstanceState){

super.onCreate (savedInstanceState);

setContentView(R.layout.activity_main);



btnLogin =(Button)findViewById(R.id.btnLogin) ;

bnnLogin.setOnClickListener(this);

}



@Override

public void onClick(查看v){



意图l =新意图(getApplicationContext(),Home.class);

startActivity(l);

}



}



HomeLauncher.java



包com.example.unisalabapp;



import android.os.Bundle;

导入android.view.View;

导入android.view.View.OnClickListener;

导入android.widget.Button;

import android.app.Activity;

import android.content.Intent;



public class HomeLauncher extends Activity实现OnClickListener {



按钮btnBooking;



@Override

protected void onCreate(Bundle) savedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.home);



//设置按钮监听器

btnB ooking =(Button)findViewById(R.id.btnBooking);

bnnBooking.setOnClickListener(this);

}

@Override

public void onClick(View v){



意图b =新意图(getApplicationContext(),ComputerBooking.class);

startActivity(b);

}

}



其他五项活动之一: < br $>


包com.example.unisalabapp;



import android.app.Activity;

导入android.os.Bundle;

导入android.view.View;

导入android.widget.Button;



公共类ComputerBooking扩展活动{



@Override

protected void onCreate(Bundle savedInstanceState){

// TODO自动生成方法存根

super.onCreate(savedInstanceState);

setContentView(R.layout.computer_booking);



//设置按钮监听器

按钮startButton =(按钮)findViewById(R.id.btnBack);

startButton.setOnClickListener(new View.OnClickListener() {

public void onClick(查看视图){

完成();

}

}); < br $>
}



}



清单:





< manifest xmlns:android =http://schemas.android.com/ apk / res / android>

package =com.example.unisalabapp

android:versionCode =1

android: versionName =1.0>



< uses-sdk>

android:minSdkVersion =8

android:targetSdkVersion =17/>



< application>

android:allowBackup =true

android:icon =@ drawable / ic_launcher

android:label =@ s tring / app_name

android:theme =@ style / AppTheme>



< activity>

android:name =com.example.unisalabapp.MainActivity

android:launchMode =singleTop

android:stateNotNeeded =true

android:label =@ string / app_name>

< intent-filter>

< action android:name =android.intent.action 。主要>

< category android:name =android.intent.category.LAUNCHER>





< activity>

android:name =com.example.unisalabapp.HomeLauncher

android:launchMode =singleTop

android: stateNotNeeded =true

android:label =@ string / app_name>

< intent-filter>

< action android:name =com.example.unisalabapp.HOME>

< action android:name =android.intent.category.DEFAULT>

< ;类别android:name =android.intent.category.LAUNCHER>







< ;活动>

android:name =com.example.unisalabapp.computer_booking

android:label =@ string / app_name>

< intent-filter>

< action android:name =android.intent.action.COMPUTER_BOOKING>

<! - < action机器人:名称= com.example.unisalabapp.COMPUTER_BOOKING > - >

< action android:name =android.intent.category.DEFAULT>








解决方案

在家庭活动中,onclick事件处理程序由多个按钮共享,你必须识别触发它的按钮才能启动正确的活动。看看这个: http://www.tutorialsbuzz.com/2014/ 02 / android-group-onclick-listener-multiple-button.html [ ^ ]

另一种方法是将独特的事件处理程序附加到xml布局上每个按钮的onclick属性上家庭活动,请查看: Android UI布局和控件 [< a href =http://www.codeproject.com/Articles/807524/Android-UI-Layouts-and-Controls#buttontarget =_ blanktitle =New Window> ^ ]

Hi there

I am new in Android development and I have been trying to launch activities from another. Let say from "Main Activity" I launch the "Home Activity" by clicking a button. Then from "Home Activity", I want to be able to launch 5 different activities based on whichever button is clicked. I have tried many resource online and none seems to work for me.
From Main to Home it is working fine but from Home to the other activities, nothing is happening.

Please assist.
PS. Below is my sample code:

MainActivity.java

package com.example.unisalabapp;

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

public class MainActivity extends Activity implements OnClickListener {

Button btnLogin;

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

btnLogin = (Button) findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(this);
}

@Override
public void onClick(View v) {

Intent l = new Intent(getApplicationContext(), Home.class);
startActivity(l);
}

}

HomeLauncher.java

package com.example.unisalabapp;

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

public class HomeLauncher extends Activity implements OnClickListener {

Button btnBooking;

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

//setup button listener
btnBooking = (Button) findViewById(R.id.btnBooking) ;
btnBooking.setOnClickListener(this);
}
@Override
public void onClick(View v) {

Intent b = new Intent(getApplicationContext(), ComputerBooking.class);
startActivity(b);
}
}

one of the five other activities:

package com.example.unisalabapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class ComputerBooking extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.computer_booking);

//setup button listener
Button startButton = (Button) findViewById(R.id.btnBack);
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
finish();
}
});
}

}

The Manifest:


<manifest xmlns:android="http://schemas.android.com/apk/res/android">
package="com.example.unisalabapp"
android:versionCode="1"
android:versionName="1.0" >

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

<application>
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >

<activity>
android:name="com.example.unisalabapp.MainActivity"
android:launchMode="singleTop"
android:stateNotNeeded="true"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN">
<category android:name="android.intent.category.LAUNCHER">


<activity>
android:name="com.example.unisalabapp.HomeLauncher"
android:launchMode="singleTop"
android:stateNotNeeded="true"
android:label="@string/app_name">
<intent-filter>
<action android:name="com.example.unisalabapp.HOME">
<action android:name="android.intent.category.DEFAULT">
<category android:name="android.intent.category.LAUNCHER">



<activity>
android:name="com.example.unisalabapp.computer_booking"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.COMPUTER_BOOKING">
<!-- <action android:name="com.example.unisalabapp.COMPUTER_BOOKING"> -->
<action android:name="android.intent.category.DEFAULT">




解决方案

On the home activity, the onclick event handler is being shared by multiple buttons, you will have to identify the button that trigger it in order to launch the correct activity. Check this out: http://www.tutorialsbuzz.com/2014/02/android-group-onclick-listener-multiple-button.html[^]
Another way is to attach a distinct event handler to the onclick attribute of each button on the xml layout of home activity, check this out: Android UI Layouts and Controls[^]


这篇关于如何从Android中的一个活动启动活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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