我是Android的新手。我正在使用Android Studio开发游戏。我有3个活动。第一个加载是主菜单。
主菜单加载。然后我可以点击两个按钮之一。一个按钮加载Activity Main,另一个按钮加载名为Multiple Choice的活动。
多项选择是问题所在。当我选择它时,使用Multiple Choice Class文件,但出于某种原因显示Activity Main。这是代码。
Manifiest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xxxxxxx.ultramath" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainMenu"
android:label="@string/title_activity_main_menu" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main_menu" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MultipleChoice"
android:label="@string/title_activity_multiple_choice" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
主菜单
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.xxxxxxx.ultramath.MainMenu">
<TextView android:text="@string/app_name" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textSize="32sp"
android:textColor="#ff000000"
android:id="@+id/textView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btnTypeAnswers"
android:id="@+id/btnTypeAnswers"
android:onClick="onClick"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="58dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btnMultipleChoice"
android:id="@+id/btnMultipleChoice"
android:layout_below="@+id/btnTypeAnswers"
android:layout_alignRight="@+id/btnTypeAnswers"
android:layout_alignEnd="@+id/btnTypeAnswers"
android:onClick="btnMultipleChoiceonClick" />
</RelativeLayout>
主菜单类
public class MainMenu extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onClick(View view)
{
Intent i = new Intent(this, MainActivity.class);
this.startActivity(i);
}
public void btnMultipleChoiceonClick(View view)
{
Intent i = new Intent(this, MultipleChoice.class);
this.startActivity(i);
}
}
多项选择活动
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MultipleChoice"
android:minWidth="30dp"
android:focusableInTouchMode="false">
<TextView android:text="@string/LeftText" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Left"
android:layout_below="@+id/Timeleft"
android:layout_toLeftOf="@+id/Operator"
android:layout_toStartOf="@+id/Operator" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/OperatorText"
android:id="@+id/Operator"
android:layout_below="@+id/Timeleft"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/RightText"
android:id="@+id/Right"
android:layout_below="@+id/Timeleft"
android:layout_toRightOf="@+id/Operator"
android:layout_toEndOf="@+id/Operator" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/StartButton"
android:id="@+id/btnStart"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="btnStart_Click"
android:enabled="true"
android:focusable="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/TimeLeft"
android:id="@+id/Timeleft"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Correct: 0"
android:id="@+id/CorrectAnswers"
android:layout_alignTop="@+id/Timeleft"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/LinearContainer"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:baselineAligned="false">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/linearLayout1"
android:layout_weight="1"
android:gravity="center_horizontal">
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/linearLayout2"
android:gravity="center_horizontal">
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/linearLayout3"
android:gravity="center_horizontal">
</LinearLayout>
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_below="@+id/LinearContainer"
android:layout_toRightOf="@+id/CorrectAnswers"
android:layout_toEndOf="@+id/CorrectAnswers"
android:layout_marginTop="65dp" />
</RelativeLayout>
多选类
public class MultipleChoice extends ActionBarActivity {
TextView Left;
TextView Operator;
TextView Right;
TextView GameTimer;
TextView CorrectAnswers;
ImageView imageView[] = new ImageView[8];
int imageViewNumber;
Button btnStart;
int intLeft;
int intOperator;
int intRight;
int intCorrectAnswer;
int intCorrectAnswers;
String strCorrectAnswer;
CountDownTimer problemtimer = new CountDownTimer(10000,1000) {
@Override
public void onTick(long millisUntilFinished) {
}
@Override
public void onFinish() {
NextProblem();
}
};
CountDownTimer gametimer = new CountDownTimer(60000,1000) {
@Override
public void onTick(long millisUntilFinished) {
GameTimer.setText("Time Left: " + millisUntilFinished / 1000);
}
@Override
public void onFinish() {
problemtimer.cancel();
gametimer.cancel();
GameTimer.setText("Time Left: 0");
btnStart.setEnabled(true);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SetStartupValues();
}
private void SetStartupValues(){
Left = (TextView) findViewById(R.id.Left);
Operator = (TextView) findViewById(R.id.Operator);
Right = (TextView) findViewById(R.id.Right);
GameTimer = (TextView) findViewById(R.id.Timeleft);
CorrectAnswers = (TextView) findViewById(R.id.CorrectAnswers);
btnStart = (Button) findViewById(R.id.btnStart);
Left.setText("");
Operator.setText("");
Right.setText("");
GameTimer.setText("Time Left: 60");
CorrectAnswers.setText("Correct: 0");
intCorrectAnswers = 0;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void btnStart_Click(View view) {
// Do something in response to button click
SetStartupValues();
NextProblem();
gametimer.start();
btnStart.setEnabled(false);
}
public void NextProblem(){
intLeft = new Random().nextInt(100);
intRight= new Random().nextInt(100);
intOperator= new Random().nextInt(3);
Left.setText(String.valueOf(intLeft));
Right.setText(String.valueOf(intRight));
imageViewNumber=0;
switch(intOperator){
case 0:
Operator.setText("+");
intCorrectAnswer = intLeft + intRight;
break;
case 1:
Operator.setText("-");
intCorrectAnswer=intLeft-intRight;
break;
case 2:
Operator.setText("X");
intCorrectAnswer=intLeft*intRight;
break;
}
strCorrectAnswer = String.valueOf(intCorrectAnswer);
GetNumbers(strCorrectAnswer);
problemtimer.start();
}
private void GetNumbers(String strNumber){
LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout1);
int L = strNumber.length();
String c;
for (int i = 1;i<L;i++){
c=strNumber.substring(i,i+1);
switch (c){
case "1":
imageView[imageViewNumber].setBackgroundResource(R.drawable.one);
ll.addView(imageView[imageViewNumber]);
break;
case "2":
imageView[imageViewNumber].setBackgroundResource(R.drawable.two);
ll.addView(imageView[imageViewNumber]);
break;
case "3":
imageView[imageViewNumber].setBackgroundResource(R.drawable.three);
ll.addView(imageView[imageViewNumber]);
break;
case "4":
imageView[imageViewNumber].setBackgroundResource(R.drawable.four);
ll.addView(imageView[imageViewNumber]);
break;
case "5":
imageView[imageViewNumber].setBackgroundResource(R.drawable.five);
ll.addView(imageView[imageViewNumber]);
break;
case "6":
imageView[imageViewNumber].setBackgroundResource(R.drawable.six);
ll.addView(imageView[imageViewNumber]);
break;
case "7":
imageView[imageViewNumber].setBackgroundResource(R.drawable.seven);
ll.addView(imageView[imageViewNumber]);
break;
case "8":
imageView[imageViewNumber].setBackgroundResource(R.drawable.eight);
ll.addView(imageView[imageViewNumber]);
break;
case "9":
imageView[imageViewNumber].setBackgroundResource(R.drawable.nine);
ll.addView(imageView[imageViewNumber]);
break;
case "0":
imageView[imageViewNumber].setBackgroundResource(R.drawable.zero);
ll.addView(imageView[imageViewNumber]);
break;
}
}
}
如果我发布了很多代码,我道歉。我不知道问题可能在哪里。
答案 0 :(得分:1)
仅在清单文件中的主启动器活动代码内提及以下行:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
因为它将遵循清单文件中的自上而下方法。