【问题标题】:Android Multiple Intents - One FormAndroid 多意图 - 一种形式
【发布时间】:2015-02-16 18:19:11
【问题描述】:

下午好,

我正在尝试在我的 andorid 应用程序中创建一个基本菜单,其中包含 5 个按钮,每个按钮将您带到另一个表单。我正在尝试创建 java 来执行此操作,但我的每个按钮似乎都遇到了以下错误

“EXAMPLE 无法解析为变量”

请帮助我解决我的代码,或者是否有更简单的方法可以让我使用 5 个按钮执行此菜单,每个按钮都有不同的形式

<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/btnFootball"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="25dp"
        android:text="Football"
        android:onClick="btnFootball" />

    <Button
        android:id="@+id/btnHockey"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btnFootball"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="25dp"
        android:text="Hockey"
        android:onClick="btnHockey" />

    <Button
        android:id="@+id/btnLacrosse"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btnLacrosse"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="25dp"
        android:text="Lacrosse"
        android:onClick="btnLacrosse" />

    <Button
        android:id="@+id/btnCurling"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btnLacrosse"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="25dp"
        android:text="Curling"
        android:onClick="btnCurling" />

    <Button
        android:id="@+id/btnLogout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btnCurling"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="25dp"
        android:text="Logout"
        android:onClick="btnLogout" />

</RelativeLayout>

这里是 Java:

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 {

    public void ButtonOnClick(View v) {
        switch (v.getId()) {
          case R.id.btnFootball:
            startActivity(Football);
            break;
          case R.id.btnHockey:
              startActivity(Hockey);
            break;
          case R.id.btnLacrosse:
              startActivity(Lacrosse);
                break;
          case R.id.btnCurling:
              startActivity(Curling);
                break;
          case R.id.btnLogout:
              startActivity(HomeActivity);
                break;
          }
    }


    }

【问题讨论】:

  • 您需要阅读 Intent 以及如何启动它们。
  • Multiple Intents on Main Menu 的可能重复项
  • 这是您的代码,还是this 您的代码?在我看来,你一遍又一遍地问同样的事情......

标签: java android button android-intent case


【解决方案1】:

@Meryl2 当你使用 ""android:onClick="btnLogout"""

那你应该有相应的方法 公共无效btnLogout(查看视图{ //你的代码在这里 }

同样适用于代码中的所有按钮

【讨论】:

    【解决方案2】:

    你的错误 ->

    您没有使用setcontentView() 也没有覆盖onCreate() 方法。还有一件事,你没有正确发送intent

    这是发送意图的方法

    Intent intent = new Intent(getApplicationContext(), DestinationActivity.class);
    startActivity(intent);
    

    【讨论】:

    • 感谢大家的所有反馈,当我开始使用一种新的编程语言时,我非常感谢您的所有帮助和建议。我选择使用用户@WannaGetHigh 方法来解决这个问题,它现在可以正常工作了!
    【解决方案3】:

    我已经修改了类和布局。您需要为每个操作创建其他类。

    public class MainActivity extends Activity implements Button.OnClickListener{
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
    
    }
    
    @Override
    public void onClick(View v) {
        int id = v.getId();
    
        Intent intent = null;
        if(id == R.id.btnHockey) {
            intent = new Intent(MainActivity.this, Hockey.class);
        } else if(id == R.id.btnCurling) {
            intent = new Intent(MainActivity.this, Curling.class);
        } else if(id == R.id.btnFootball) {
            intent = new Intent(MainActivity.this, Football.class);
        } else if(id == R.id.btnLogout) {
            intent = new Intent(MainActivity.this, Logout.class);
        } else if(id == R.id.btnLacrosse) {
            intent = new Intent(MainActivity.this, Lacrosse.class);
        }
    
        startActivity(intent);
    }
    

    }

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"/>
    
    <Button
        android:id="@+id/btnFootball"
        android:layout_below="@id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:text="Football"
        android:onClick="onClick" />
    
    <Button
        android:id="@+id/btnHockey"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btnFootball"
        android:layout_marginTop="25dp"
        android:text="Hockey"
        android:onClick="onClick" />
    
    <Button
        android:id="@+id/btnLacrosse"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btnHockey"
        android:layout_marginTop="25dp"
        android:text="Lacrosse"
        android:onClick="onClick" />
    
    <Button
        android:id="@+id/btnCurling"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btnLacrosse"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="25dp"
        android:text="Curling"
        android:onClick="onClick" />
    
    <Button
        android:id="@+id/btnLogout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btnCurling"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="25dp"
        android:text="Logout"
        android:onClick="onClick" />
    

    【讨论】:

      【解决方案4】:

      我想我有你的问题。在每个 Button 中,您设置单击它时将使用的方法的名称:

      android:onClick="btnCurling"

      android:onClick="btnHockey"

      ...

      但是你这里只有一种方法:

      public void ButtonOnClick(View v) {
          ...
      }
      

      一种解决方案是像这样定义每个方法:

      public class MainMenu extends Activity {
      
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
      
              // here you tell your activity what layout he will display
              setContentView(R.layout.TheNameOfYourLayout);
          }
      
          // it will get there when you click on the "@+id/btnHockey" Button
          public void btnHockey(View v) {
              Intent intent = new Intent(this, NameOfTheActivityToLaunch.class);
              startActivity(intent);
          }
      
          // then you add the other ones btnCurling, ...
      }
      

      另一种解决方案是以编程方式设置每个按钮的侦听器:

      public class MainMenu extends Activity implements View.OnClickListener {
      
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
      
              // Here you tell your activity what layout he will display
              setContentView(R.layout.TheNameOfYourLayout);
      
              // This will find your button in the layout you just set 
              // and then set this Class as your Listener
              findViewById(R.id.btnFootball).setOnClickListener(this);
              
              // Then set the listener of all your other buttons
          }
      
          @Override
          public void onClick(View v) {
              
              switch (v.getId()) {
                  case R.id.btnFootball:
                      Intent intent = new Intent(this, NameOfTheActivityToLaunch.class);
                      startActivity(intent);
                      break;
                  case R.id.btnHockey:
                      Intent intent = new Intent(this, NameOfTheActivityToLaunch.class);
                      startActivity(intent);
                      break;
                  
                  // ...
              }   
          }
      }
      

      您还必须删除 XML 布局文件中的所有 android:onClick="btnCurling" 行,此解决方案才能正常工作。

      希望它有助于欢呼:)

      【讨论】:

        【解决方案5】:

        试试这个代码;

        public class MainMenu extends Activity implements OnClickListener {
        Button btn_football,btn_hokey,btn_something; // add another button here
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.yourlayout);
            btn_football = (Button)findViewById(R.id.btnFootball);
            // add another button here same way
            btn_football.setOnClickListener(this); 
            // add another button here same way 
           @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch(v.getId()){
            case R.id.btnFootball:
                Intent intent = new Intent(MainMenu.this,(nextclass_addhere).class);
                startActivity(intent);
                break;
            case R.id.another button:
              Intent intent = new Intent(MainMenu.this,(nextclass_addhere).class);
               startActivity(intent);  
            break;
            //.......add here another button with same way
         }
        
        }
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-11-25
          相关资源
          最近更新 更多