Android 生成随机数小程序 - Material Design 和动画效果
要制作一个输入 a 和 b,然后输出 a~b 之间的随机数的小程序,我们可以使用 Android Studio 进行开发。下面是一个简单的示例代码,使用了 Material Design 风格和一些动效:
- 首先,在'build.gradle'文件中添加 Material Design 的支持库:
implementation 'com.google.android.material:material:1.5.0'
-
创建一个新的 Activity,命名为'MainActivity'。
-
在'activity_main.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:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="输入范围(a 和 b 之间的整数)"
android:textSize="18sp"
android:textColor="@android:color/black"
android:layout_marginBottom="16dp"/>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hintEnabled="true"
app:hint="a">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/editTextA"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hintEnabled="true"
app:hint="b"
android:layout_marginTop="16dp">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/editTextB"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>
<Button
android:id="@+id/generateButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="生成随机数"
android:textColor="@android:color/white"
android:background="@color/colorPrimary"
android:layout_marginTop="16dp"/>
<TextView
android:id="@+id/resultText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:textColor="@android:color/black"
android:layout_marginTop="16dp"/>
</LinearLayout>
- 在'MainActivity.java'类中添加以下代码:
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.material.textfield.TextInputEditText;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private TextInputEditText editTextA, editTextB;
private Button generateButton;
private TextView resultText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextA = findViewById(R.id.editTextA);
editTextB = findViewById(R.id.editTextB);
generateButton = findViewById(R.id.generateButton);
resultText = findViewById(R.id.resultText);
generateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String aString = editTextA.getText().toString();
String bString = editTextB.getText().toString();
if (aString.isEmpty() || bString.isEmpty()) {
Toast.makeText(MainActivity.this, '请输入范围', Toast.LENGTH_SHORT).show();
return;
}
int a = Integer.parseInt(aString);
int b = Integer.parseInt(bString);
if (a >= b) {
Toast.makeText(MainActivity.this, '请输入有效的范围', Toast.LENGTH_SHORT).show();
return;
}
Random random = new Random();
int randomNum = random.nextInt(b - a + 1) + a;
resultText.setText(String.valueOf(randomNum));
}
});
}
}
这个示例代码中,我们使用了'TextInputLayout'来创建带有提示的输入框,使用了'Button'来生成随机数,并在'TextView'中显示结果。
同时,我们使用了'Random'类来生成随机数。注意,为了确保输入的范围有效,我们进行了一些简单的验证。
你可以根据自己的需求进一步自定义界面和动效,例如添加过渡动画、颜色渐变效果等。希望这可以帮到你!
原文地址: https://www.cveoy.top/t/topic/qtFF 著作权归作者所有。请勿转载和采集!