我想制作一个游戏,首先必须选择玩家数量。默认玩家数量为2,但我为2个以上的玩家设计了4个Edittext,并使玩家编号3和编号4不可见。 当您选择3或4播放器时,我想使它们再次可见,但是当您选择3或4时程序崩溃。
主要活动XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_marginTop="50dp"
android:textColor="#f10404"
android:textSize="30sp"
android:layout_gravity="center"
android:text="Friendly Challenge"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_marginTop="50dp"
android:textColor="#43b12d"
android:textSize="20sp"
android:layout_gravity="center"
android:text="Please Select The Number of Players"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn2"
android:text="2"
android:layout_marginTop="30dp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn3"
android:text="3"
android:layout_marginTop="30dp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn4"
android:text="4"
android:layout_marginTop="30dp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
MainActivity Java:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button bt2,bt3,bt4;
EditText pl3,pl4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt2=findViewById(R.id.btn2);
bt3=findViewById(R.id.btn3);
bt4=findViewById(R.id.btn4);
pl3=findViewById(R.id.player3);
pl4=findViewById(R.id.player4);
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
finish();
}
});
bt3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
pl3.setVisibility(View.VISIBLE);
startActivity(intent);
finish();
}
});
bt4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
finish();
}
});
}
}
Main2Activity XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".Main2Activity">
<EditText
android:id="@+id/player1"
android:hint="Playe Number 1"
android:layout_marginTop="30dp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/player2"
android:hint="Playe Number 2"
android:layout_marginTop="30dp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/player3"
android:visibility="invisible"
android:hint="Player Number 3"
android:layout_marginTop="30dp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/player4"
android:visibility="invisible"
android:hint="Player Number 3"
android:layout_marginTop="30dp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnstart"
android:text="Start"
android:layout_marginTop="30dp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Main2Activity java:
package com.example.iman_pc.challenge;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
public class Main2Activity extends AppCompatActivity {
Button bt3,bt4;
EditText pl3,pl4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
bt3=findViewById(R.id.btn3);
bt4=findViewById(R.id.btn4);
pl3=findViewById(R.id.player3);
pl4=findViewById(R.id.player4);
}
}