實作功能:總共分三個模式
普通模式:數字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>
效果圖展示:
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/259509.html
標籤:其他
上一篇:Codeforces Round #701 (Div. 2) C. Floor and Mod 整除分塊
下一篇:imageview顯示bitmap出現黑屏卡死的問題,就是調取攝像頭,將獲取的bitmap通過遍歷找到出現最多的顏色,再將bitmap顯示出來
