Android 多个意图 - 一个表单
Android Multiple Intents - One Form
下午好,
我正在尝试在我的 andorid 应用程序中创建一个基本菜单,其中包含 5 个按钮,每个按钮将您带到另一个表单。我正在尝试创建 java 来执行此操作,但似乎 运行 我的每个按钮都出现以下错误
"EXAMPLE cannot be resolved as a variable"
请帮助我解决我的代码,或者是否有更简单的方法允许我执行这个菜单,每个按钮有 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;
}
}
}
你的错误 ->
您没有使用 setcontentView()
也没有覆盖 onCreate()
方法。还有一件事,您没有正确发送 intent
。
这是发送意图的方法
Intent intent = new Intent(getApplicationContext(), DestinationActivity.class);
startActivity(intent);
试试这个代码;
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
}
}
}
我已经修改了 class 和布局。您需要为每个操作创建其他 classes。
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" />
@Meryl2
当你使用
""android:onClick="btnLogout"""
那你应该有相应的方法
public void btnLogout(查看查看{
//你的代码在这里
}
同样适用于您代码中的所有按钮
我想我明白了你的问题。在每个按钮中,您设置单击它时将使用的方法的名称:
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"
行才能使此解决方案生效。
希望对大家有所帮助:)
下午好,
我正在尝试在我的 andorid 应用程序中创建一个基本菜单,其中包含 5 个按钮,每个按钮将您带到另一个表单。我正在尝试创建 java 来执行此操作,但似乎 运行 我的每个按钮都出现以下错误
"EXAMPLE cannot be resolved as a variable"
请帮助我解决我的代码,或者是否有更简单的方法允许我执行这个菜单,每个按钮有 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;
}
}
}
你的错误 ->
您没有使用 setcontentView()
也没有覆盖 onCreate()
方法。还有一件事,您没有正确发送 intent
。
这是发送意图的方法
Intent intent = new Intent(getApplicationContext(), DestinationActivity.class);
startActivity(intent);
试试这个代码;
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
}
}
}
我已经修改了 class 和布局。您需要为每个操作创建其他 classes。
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" />
@Meryl2 当你使用 ""android:onClick="btnLogout"""
那你应该有相应的方法 public void btnLogout(查看查看{ //你的代码在这里 }
同样适用于您代码中的所有按钮
我想我明白了你的问题。在每个按钮中,您设置单击它时将使用的方法的名称:
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"
行才能使此解决方案生效。
希望对大家有所帮助:)