在主菜单中多意图 [英] Multiple Intents on Main Menu

查看:203
本文介绍了在主菜单中多意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下午好

我是一个新的用户到Android eclipse.I已经创建了3按钮的菜单,这将导致用户到给定他们的选择3单独的页面,每个页面给出了自己的意图不过我得到我的code许多错误,我想知道我在做了正确的方式,如果没有哪种方式我应该接近这个简单的主菜单

更新:现在有似乎是在运行code没有错误,不过仍然按键反应迟钝

XML:

 <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:paddingBottom会=@扪/ activity_vertical_margin
    机器人:paddingLeft =@扪/ activity_horizo​​ntal_margin
    机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
    机器人:paddingTop =@扪/ activity_vertical_margin
    工具:上下文=com.techblogon.loginexample.MainMenu>    < ImageView的
        机器人:ID =@ + ID / imageView1
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentTop =真
        机器人:layout_centerHorizo​​ntal =真
        机器人:SRC =@绘制/ PIC/>    <按钮
        机器人:ID =@ + ID / btnWorkout
        机器人:layout_width =150dp
        机器人:layout_height =60dp
        机器人:layout_alignLeft =@ + ID / btnhealth
        机器人:layout_below =@ + ID / imageView1
        机器人:layout_marginTop =36dp
        机器人:文字=登录/查看锻炼/>    <按钮
        机器人:ID =@ + ID / btnhealth
        机器人:layout_width =150dp
        机器人:layout_height =60dp
        机器人:layout_centerHorizo​​ntal =真
        机器人:layout_centerVertical =真
        机器人:文字=健康小贴士/>    <按钮
        机器人:ID =@ + ID / btnLogout
        机器人:layout_width =150dp
        机器人:layout_height =60dp
        机器人:layout_alignLeft =@ + ID / btnhealth
        机器人:layout_below =@ + ID / btnhealth
        机器人:layout_marginTop =22dp
        机器人:文字=注销/>< / RelativeLayout的>

Java的类:

 包com.techblogon.loginexample;进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.Button;
进口android.view.View.OnClickListener;
进口android.content.Context;
进口android.view.Menu;
进口android.view.MenuItem;公共类MainMenu的延伸活动{    私人上下文的背景下;    保护无效的onCreate(捆绑setInstanceState){
        super.onCreate(setInstanceState);
        上下文=这一点;        的setContentView(R.layout.activity_main_menu);    }    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        返回true;
    }    公共布尔btnhealth(查看视图){
        意图I =新意图(背景下,Tips.class);
        startActivity(ⅰ);
        返回true;    }
    公共布尔btnWorkout(查看视图){
        意图II =新意图(背景下,Workout.class);
        startActivity(二);
        返回true;
    }
    公共布尔btnLogout(查看视图){
        意图III =新意图(背景下,Tips.class);
        startActivity(三);
        返回true;
    }
}


解决方案

您应该使用处理方法onClick事件:

 <按钮
...
       安卓的onClick =handleClick()
.../>

在code:

 公共类MainMenu的延伸活动{...公共无效handleClick(查看视图){
//做的东西
}}

编辑:

更具体的答案,给出你的code:

  ...
<按钮
        机器人:ID =@ + ID / btnWorkout
        机器人:layout_width =150dp
        机器人:layout_height =60dp
        机器人:layout_alignLeft =@ + ID / btnhealth
        机器人:layout_below =@ + ID / imageView1
        机器人:layout_marginTop =36dp
        机器人:文字=登录/查看锻炼
        安卓的onClick =btnWorkout/>    <按钮
        机器人:ID =@ + ID / btnhealth
        机器人:layout_width =150dp
        机器人:layout_height =60dp
        机器人:layout_centerHorizo​​ntal =真
        机器人:layout_centerVertical =真
        机器人:文字=健康小贴士
        安卓的onClick =btnhealth/>    <按钮
        机器人:ID =@ + ID / btnLogout
        机器人:layout_width =150dp
        机器人:layout_height =60dp
        机器人:layout_alignLeft =@ + ID / btnhealth
        机器人:layout_below =@ + ID / btnhealth
        机器人:layout_marginTop =22dp
        机器人:文字=注销
        安卓的onClick =btnLogout/>
...

在Android的属性值:onclick属性是方法,它会寻找,在相应的Java code为活动...(记住方法的签名的名称必须正确一个按钮单击事件处理程序的签名,看看对于Android的文档。)

我建议你选择一个不同的方法名称,以便您没有得到ID和方法的名字迷惑,因为他们是两个不同的引用

Good afternoon,

I am a new user to Android eclipse.I have created a Menu with 3 buttons which will lead the user onto 3 seperate pages given their choice, each given their own intent however I am getting many errors in my code and am wondering am I doing it the correct way and if not which way should I approach this simple main menu

UPDATE: There now appears to be no errors in running the code however the buttons remain unresponsive

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: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="com.techblogon.loginexample.MainMenu" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/PIC" />

    <Button
        android:id="@+id/btnWorkout"
        android:layout_width="150dp"
        android:layout_height="60dp"
        android:layout_alignLeft="@+id/btnhealth"
        android:layout_below="@+id/imageView1"
        android:layout_marginTop="36dp"
        android:text="Log/View Workout" />

    <Button
        android:id="@+id/btnhealth"
        android:layout_width="150dp"
        android:layout_height="60dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Health Tips" />

    <Button
        android:id="@+id/btnLogout"
        android:layout_width="150dp"
        android:layout_height="60dp"
        android:layout_alignLeft="@+id/btnhealth"
        android:layout_below="@+id/btnhealth"
        android:layout_marginTop="22dp"
        android:text="Logout" />

</RelativeLayout>

Java Class:

package com.techblogon.loginexample;

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





public class MainMenu extends Activity {

    private Context context;

    protected void onCreate(Bundle setInstanceState) {
        super.onCreate(setInstanceState);
        context=this;

        setContentView(R.layout.activity_main_menu);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        return true;
    }

    public boolean btnhealth(View view){
        Intent i = new Intent(context, Tips.class);
        startActivity(i);
        return true;

    }
    public boolean btnWorkout(View view){
        Intent ii = new Intent(context, Workout.class);
        startActivity(ii);
        return true;
    }
    public boolean btnLogout(View view){
        Intent iii = new Intent(context, Tips.class);
        startActivity(iii);
        return true;
    }
}

解决方案

You should use a handler method for the onClick event:

<Button
...  
       android:onClick="handleClick()
..."/>

in code:

public class MainMenu extends Activity {

...

public void handleClick(View view){
//do stuff
}

}

edit:

more concrete answer, given your code:

...
<Button
        android:id="@+id/btnWorkout"
        android:layout_width="150dp"
        android:layout_height="60dp"
        android:layout_alignLeft="@+id/btnhealth"
        android:layout_below="@+id/imageView1"
        android:layout_marginTop="36dp"
        android:text="Log/View Workout" 
        android:onClick="btnWorkout"/>

    <Button
        android:id="@+id/btnhealth"
        android:layout_width="150dp"
        android:layout_height="60dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Health Tips" 
        android:onClick="btnhealth"/>

    <Button
        android:id="@+id/btnLogout"
        android:layout_width="150dp"
        android:layout_height="60dp"
        android:layout_alignLeft="@+id/btnhealth"
        android:layout_below="@+id/btnhealth"
        android:layout_marginTop="22dp"
        android:text="Logout"
        android:onClick="btnLogout"/>
...

the attribute value of the android:onClick attribute is the name of the method that it will look for, in the corresponding java code for the activity...(keep in mind the signature of the method must the correct signature for a button click event handler, look at android docs for that ..)

I suggest you pick a different method name so that you don't get the id and the method name confused, as they are two separate references

这篇关于在主菜单中多意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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