我想在kotlin中从用户获得一个数字,将该数字同步到rowCount和columnCount,并创建与用户输入的数字一样多的行和列。创建后,我想在网格布局中放置1到10之间的随机数,但是当按下按钮时,网格不向布局添加行和列,它保持原样。我需要你的帮助。我的代码:
<GridLayout
android:id="@+id/gridLyt"
style="@style/GridBorder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="@id/gridLyt2"
app:layout_constraintTop_toBottomOf="@id/linearLyt">
下面的代码是我的按钮和editText所在的linearLayout。我不知道是不是因为线性布局,我的按钮不工作,我不明白
<LinearLayout
android:id="@+id/linearLyt"
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:weightSum="4">
<TextView
android:id="@+id/tv_adet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:fontFamily="@font/montserrat"
android:paddingRight="10dp"
android:layout_marginStart="7dp"
android:layout_marginTop="5dp"
android:text="@string/adet"
android:layout_weight="1"
android:textColor="@color/black"
android:textSize="25dp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.05"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/piece"
style="@style/adetTxtLayout"
android:layout_width="83dp"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:hint="@string/adetTxt"
app:layout_constraintStart_toEndOf="@+id/adet"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/etNumber"
style="@style/adetTxt"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</com.google.android.material.textfield.TextInputEditText>
</com.google.android.material.textfield.TextInputLayout>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btncalculate"
style="@style/ButtonGrey"
android:layout_width="90dp"
android:layout_height="50dp"
android:layout_marginStart="7dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="7dp"
android:layout_weight="1"
android:text="@string/hesapla"
app:layout_constraintEnd_toStartOf="@+id/btn_login2"
app:layout_constraintStart_toEndOf="@+id/piece"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btn_login2"
style="@style/ButtonGrey"
android:layout_weight="1"
android:layout_width="90dp"
android:layout_height="50dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="7dp"
android:text="@string/bul"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
这是我的MainActivity:
class MainActivity : AppCompatActivity() {
private lateinit var binding : ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.btnCalculate.setOnClickListener {
val num = binding.inpiece.text.toString().toIntOrNull() ?: return@setOnClickListener
if (num < 1 || num > 10) return@setOnClickListener
val textView = TextView(this)
textView.text = "Hello, World!"
binding.gridLyt.addView(textView)
binding.gridLyt.apply {
rowCount = num
columnCount = num
removeAllViews()
for (i in 0 until num) {
for (j in 0 until num) {
val value = (1..10).random()
val textView = TextView(context).apply {
text = value.toString()
textSize = 24f
textAlignment = View.TEXT_ALIGNMENT_CENTER
}
addView(textView, GridLayout.LayoutParams().apply {
rowSpec = GridLayout.spec(i, 1f)
columnSpec = GridLayout.spec(j, 1f)
width = 0
height = GridLayout.LayoutParams.WRAP_CONTENT
})
}
}
}
}
}
}
我试过这个代码,但它从来没有添加元素到网格布局
编辑:顺便说一句,我也试过调整网格布局的大小并启用ViewBinding。
你不需要执行两个循环。
我从你的问题中理解,当用户添加任何数字时,你需要在网格布局中相同的行和列大小。
你需要在你的代码中做这些改变。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".DynamicGridLayout">
<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/editTextUserInput"
android:layout_width="@dimen/dp_0"
android:layout_height="wrap_content"
android:hint="Enter Number"
app:layout_constraintEnd_toStartOf="@+id/buttonCalculate"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/buttonCalculate"
android:layout_width="@dimen/dp_0"
android:layout_height="wrap_content"
android:text="@string/calculate"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<GridLayout
android:id="@+id/gridLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_10"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextUserInput"
tools:context=".DynamicGridLayout" />
</androidx.constraintlayout.widget.ConstraintLayout>
- 您的kt文件如下
package com.example.gamazingtask
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.*
import android.widget.TextView
import com.example.gamazingtask.databinding.ActivityDynamicGridLayoutBinding
class DynamicGridLayout : AppCompatActivity() {
private lateinit var binding: ActivityDynamicGridLayoutBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityDynamicGridLayoutBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.buttonCalculate.setOnClickListener {
val num = binding.editTextUserInput.text.toString().toInt()
binding.gridLayout.apply {
binding.gridLayout.removeAllViews()
rowCount = num
columnCount = num
for (i in 0 until num * num) {
val value = (1 .. 10).random()
val textView = TextView(context).apply {
text = value.toString()
textSize = 24f
gravity = Gravity.CENTER
}
binding.gridLayout.addView(textView, i)
}
}
}
}
}
更改只应用于循环,宽度和高度现在可以自己管理。
逻辑在上面。
你需要改变only循环,因为你需要和用户输入的数字一样多的行和列,所以你需要运行这个循环两次用户输入的数字。