实现功能:总共分三个模式
普通模式:数字0-100 困难模式:数字0-1000 地狱模式:数字0-10000
输入数字,会提示大还是小,猜对了,三种提示,还会有猜测数字显示
MainActivity.java代码如下:
public class MainActivity extends AppCompatActivity {
private Button General,Diffculty,Hell;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
InitView();
Listener();
}
private void InitView(){
General = findViewById(R.id.general);
Diffculty = findViewById(R.id.difficulty);
Hell = findViewById(R.id.hell);
}
private void Listener(){
OnClick onClick = new OnClick();
General.setOnClickListener(onClick);
Diffculty.setOnClickListener(onClick);
Hell.setOnClickListener(onClick);
}
private class OnClick implements View.OnClickListener{
@Override
public void onClick(View v) {
Intent intent = null;
switch (v.getId()){
case R.id.general:
intent = new Intent(MainActivity.this,General.class);
break;
case R.id.difficulty:
intent = new Intent(MainActivity.this,Difficulty.class);
break;
case R.id.hell:
intent = new Intent(MainActivity.this,Hell.class);
break;
}
startActivity(intent);
}
}
}
activity_main.xml:效果图如下:
activity_main.xml:代码如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="模式选择"
android:textSize="20sp"
android:gravity="center"
android:textColor="#000"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000"
android:layout_marginTop="40dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="70dp">
<Button
android:id="@+id/general"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="普通模式(1-100)" />
<Button
android:id="@+id/difficulty"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="困难模式(1-1000)"
android:layout_gravity="center"
android:layout_marginLeft="60dp"/>
<Button
android:id="@+id/hell"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="地狱模式(1-10000)"
android:layout_marginLeft="50dp"/>
</LinearLayout>
</RelativeLayout>
建立三个活动分别为General,Difficulty,Hell,由于代码相似,只展示普通模式的代码,其余俩只更改随机函数中的数值范围即可
General.java代码如下:
public class General extends AppCompatActivity {
private EditText Number;
private Button Submit;
private TextView GuessNumberResult, GuessNumberTimes;
private int intGuessNumber = 0;
private int NumberOfGuesses = 0;
private int RandomNumber = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_general);
Number = findViewById(R.id.Number);
Submit = findViewById(R.id.submit);
GuessNumberResult = findViewById(R.id.GuessNumberResult);
GuessNumberTimes = findViewById(R.id.GuessNumberTimes);
RandomNumber = (int) (Math.random()*(100-1+1));
Submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
String StrGuessNumber = Number.getText().toString();
try{
intGuessNumber = Integer.parseInt(StrGuessNumber);
}catch (NumberFormatException e)
{
e.printStackTrace();
}
if (intGuessNumber < RandomNumber )
{
NumberOfGuesses++;
GuessNumberResult.setVisibility(View.VISIBLE);
GuessNumberTimes.setVisibility(View.VISIBLE);
GuessNumberResult.setText("猜的数字偏小");
GuessNumberTimes.setText("猜测次数:"+NumberOfGuesses);
}
if (intGuessNumber > RandomNumber )
{
NumberOfGuesses++;
GuessNumberResult.setVisibility(View.VISIBLE);
GuessNumberTimes.setVisibility(View.VISIBLE);
GuessNumberResult.setText("猜的数字偏大");
GuessNumberTimes.setText("猜测次数:"+NumberOfGuesses);
}
if (intGuessNumber == RandomNumber )
{
NumberOfGuesses++;
GuessNumberResult.setVisibility(View.VISIBLE);
GuessNumberTimes.setVisibility(View.VISIBLE);
GuessNumberResult.setText("恭喜你,猜对了");
GuessNumberTimes.setText("猜测次数:"+NumberOfGuesses);
}
}
});
}
}
activity_general.xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context=".General"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="普通模式"
android:textSize="20sp"
android:gravity="center"
android:layout_marginTop="10dp"
android:textColor="#000"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000"
android:layout_marginTop="10dp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/Tips"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Please enter the number you selected:"
android:layout_marginTop="100dp"/>
<EditText
android:id="@+id/Number"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_marginTop="90dp"
android:layout_alignRight="@+id/Tips"
android:layout_marginRight="50dp"/>
</RelativeLayout>
<Button
android:id="@+id/submit"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="Submit"
android:textAllCaps="false"
android:layout_marginTop="30dp"
android:layout_gravity="center"/>
<TextView
android:id="@+id/GuessNumberResult"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:visibility="invisible"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#ff0000"/>
<TextView
android:id="@+id/GuessNumberTimes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="invisible"
android:layout_marginTop="20dp"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#ff0000"/>
</LinearLayout>
效果图展示:


