Android
 Computer >> コンピューター >  >> プログラミング >> Android

Kotlinを使用してAndroidで特定の範囲の乱数を生成するにはどうすればよいですか?


この例は、Kotlinを使用してAndroidで特定の範囲の乱数を生成する方法を示しています

ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。

ステップ2 −次のコードをres / layout/activity_main.xmlに追加します。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
   xmlns:tools="https://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".MainActivity">
   <TextView
      android:id="@+id/text"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="50dp"
      android:text="Tutorials Point"
      android:textAlignment="center"
      android:textColor="@android:color/holo_green_dark"
      android:textSize="32sp"
      android:textStyle="bold" />
   <Button
      android:id="@+id/btn_generate"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@id/editTextMax"
      android:layout_centerInParent="true"
      android:layout_marginTop="5dp"
      android:text="GENERATE" />
   <EditText
      android:id="@+id/editTextMin"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@id/text"
      android:layout_centerInParent="true"
      android:layout_marginTop="50dp"
      android:ems="10"
      android:hint="Minimum"
      android:inputType="number" />
   <EditText
      android:id="@+id/editTextMax"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@id/editTextMin"
      android:layout_centerHorizontal="true"
      android:ems="10"
      android:hint="Maximum"
      android:inputType="number" />
   <TextView
      android:id="@+id/textViewResult"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@id/btn_generate"
      android:layout_centerInParent="true"
      android:layout_marginTop="10dp"
      android:hint="Output"
      android:text=""
      android:textColor="@android:color/black"
      android:textSize="24sp"
      android:textStyle="bold" />
</RelativeLayout>

ステップ3 −次のコードをsrc / MainActivity.kt

に追加します
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import java.util.*
class MainActivity : AppCompatActivity() {
   lateinit var editTextMin: EditText
   lateinit var editTextMax: EditText
   lateinit var button: Button
   lateinit var textView: TextView
   private var min = 0
   private var max: Int = 0
   private var output: Int = 0
   override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main)
      title = "KotlinApp"
      val random = Random()
      editTextMin = findViewById(R.id.editTextMin)
      editTextMax = findViewById(R.id.editTextMax)
      button = findViewById(R.id.btn_generate)
      textView = findViewById(R.id.textViewResult)
      button.setOnClickListener {
         val tempMin: String = editTextMin.text.toString()
         val tempMax: String = editTextMax.text.toString()
         if (tempMin != "" && tempMax != "") {
            min = tempMin.toInt()
            max = tempMax.toInt()
            if (max > min) {
               output = random.nextInt(max − min + 1) + min textView.text = "" + output
            }
         }
      }
   }
}

ステップ4 −次のコードをandroidManifest.xmlに追加します

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="com.example.q11">
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">
      <activity android:name=".MainActivity">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

アプリケーションを実行してみましょう。実際のAndroidモバイルデバイスをコンピューターに接続していると思います。 Android Studioからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、[実行]アイコンをクリックします ツールバーからKotlinを使用してAndroidで特定の範囲の乱数を生成するにはどうすればよいですか? 。オプションとしてモバイルデバイスを選択し、デフォルトの画面を表示するモバイルデバイスを確認します

Kotlinを使用してAndroidで特定の範囲の乱数を生成するにはどうすればよいですか?


  1. Android sqliteでrandom()を使用する方法は?

    例に入る前に、Androidのsqliteデータベースが何であるかを知っておく必要があります。 SQLiteは、デバイス上のテキストファイルにデータを保存するオープンソースのSQLデータベースです。 Androidには、SQLiteデータベースの実装が組み込まれています。 SQLiteは、すべてのリレーショナルデータベース機能をサポートしています。このデータベースにアクセスするために、JDBC、ODBCなどのデータベースへの接続を確立する必要はありません。 この例は、Android sqliteでrandom()を使用する方法について示しています ステップ1 − Android Studi

  2. Androidで特定の範囲の乱数を生成するにはどうすればよいですか?

    この例は、Androidで特定の範囲の乱数を生成する方法を示しています。 ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。 ステップ2 −次のコードをres / layout/activity_main.xmlに追加します。 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout    xmlns:android=&quo