我想做一个简单的数学游戏,有两种模式,加法和减法。我想出了如何创建一个按钮,将"添加按钮"链接到添加活动,但我似乎不知道如何创建第二个"减法按钮",将链接到减法活动。下面是我的代码:
package com.example.kirky_000.madmath;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Context;
import android.content.Intent;
public class MainMenu extends ActionBarActivity {
Button button;
Button button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.button);
button2 = (Button) findViewById(R.id.button2);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Addition.class);
startActivity(intent);
}
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Subtraction.class);
startActivity(intent);
}
});
}
你的代码只是有一些语法错误,这是解决每个给定的代码…
package com.example.kirky_000.madmath;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Context;
import android.content.Intent;
public class MainMenu extends ActionBarActivity {
Button button;
Button button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.button);
button2 = (Button) findViewById(R.id.button2);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Addition.class);
startActivity(intent);
}
});
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Subtraction.class);
startActivity(intent);
}
});
}
}
第二个添加监听器的指令在第一个OnClickListener中。
因此侦听器不会添加到第二个按钮。你的代码应该像这样:
addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.button);
button2 = (Button) findViewById(R.id.button2);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Addition.class);
startActivity(intent);
}
});
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Subtraction.class);
startActivity(intent);
});
}
您需要为不同的按钮设置不同的单击侦听器。现在,你把第二个按钮的点击监听器放在第一个按钮的点击监听器中。像这样把它们分开…
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Addition.class);
startActivity(intent);
}
});
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Subtraction.class);
startActivity(intent);
}
)};
解决方案之前的一些要点(这些将有助于您进一步编码)
-
在按钮声明后声明
context
,因此在Button button, button2
后写入:final Context context;
-
始终记住:始终在
onCreate()
中初始化对象方法。在OnCreate()
之后setContentView(R.layout.activity_main_menu);
, write:button = (Button) findViewById(R.id.button); button2 = (Button) findViewById(R.id.button2); //then define context context = MainMenu.this; //or context = getApplicationContext();
-
现在
addListenerOnButton()
函数将是这样的(只需替换)addListenerOnButton()
和以下代码):public void addListenerOnButton(){ button.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent = new Intent(context, Addition.class); startActivity(intent); } }); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent = new Intent(context, Subtraction.class); startActivity(intent); } }); }
-
现在你的代码中的wong是什么?
你的onClick listener代码是错误的
享受编码:)(和android:))…
你甚至可以这样做
public void mainClickHandler(View v)
{
switch (v.getId()) {
case R.id.button:
Intent intent = new Intent(context, Addition.class);
startActivity(intent);
case R.id.button2:
Intent intent = new Intent(context, Subtraction.class);
startActivity(intent);
}
}