Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
其中';他是从哪拿电话号码的?自定义数字键盘(Android Kotlin)_Android_Kotlin - Fatal编程技术网

其中';他是从哪拿电话号码的?自定义数字键盘(Android Kotlin)

其中';他是从哪拿电话号码的?自定义数字键盘(Android Kotlin),android,kotlin,Android,Kotlin,我有一个我一直在寻找的小程序,例如,其中创建了一个数字个性化键盘,对此我有几个疑问: import android.os.Bundle import android.text.Editable import android.view.View import android.widget.TextView import androidx.appcompat.app.AppCompatActivity import kotlinx.android.synthetic.main.activity_ma

我有一个我一直在寻找的小程序,例如,其中创建了一个数字个性化键盘,对此我有几个疑问:

import android.os.Bundle
import android.text.Editable
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity(), View.OnClickListener {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initViews()
    }

    private fun initViews() {
        t9_key_0.setOnClickListener(this)
        t9_key_1.setOnClickListener(this)
        t9_key_2.setOnClickListener(this)
        t9_key_3.setOnClickListener(this)
        t9_key_4.setOnClickListener(this)
        t9_key_5.setOnClickListener(this)
        t9_key_6.setOnClickListener(this)
        t9_key_7.setOnClickListener(this)
        t9_key_8.setOnClickListener(this)
        t9_key_9.setOnClickListener(this)
        t9_key_backspace.setOnClickListener(this)
        t9_key_clear.setOnClickListener(this)
    }

    override fun onClick(v: View?) {

        if (v?.tag != null && "number_button" == v.tag) {

            password_field.append((v as TextView).text)
            return

        }
        when (v?.id) {

            R.id.t9_key_clear -> {

                password_field.text = null

            }
            R.id.t9_key_backspace -> {

                val editable: Editable = password_field.text
                val charCount = editable.length

                if (charCount > 0) {

                    editable.delete(charCount - 1, charCount)

                }
            }
        }
    }
}
以下是我的风格:

<style name="keyboard_row">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:divider">@drawable/keyboard_divider</item>
    <item name="android:gravity">center</item>
    <item name="android:showDividers">beginning|middle|end</item>
</style>

<style name="keyboard_button">
    <item name="android:layout_width">0dp</item>
    <item name="android:layout_height">match_parent</item>
    <item name="android:layout_weight">1</item>
    <item name="android:paddingTop">12dp</item>
    <item name="android:paddingBottom">12dp</item>
    <item name="android:clickable">true</item>
    <item name="android:gravity">center</item>
    <item name="android:scaleType">centerInside</item>
    <item name="android:background">@drawable/keyboard_button_bg</item>
    <item name="android:textAppearance">?android:attr/textAppearanceLarge</item>
</style>

<style name="keyboard_number_button" parent="keyboard_button">
    <item name="android:tag">number_button</item>
</style>

匹配父项
包装内容
@可拉拔/键盘分隔器
居中
开始|中间|结束
0dp
匹配父项
1.
12dp
12dp
真的
居中
中心内
@可拖动/键盘按钮
?android:attr/textAppearanceLarge
数字按钮
这是我的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dp"
android:orientation="vertical"
android:padding="8dp">

<EditText
    android:id="@+id/password_field"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:background="#eeeeee"
    android:enabled="false"
    android:minHeight="48dp"
    android:paddingLeft="8dp"
    android:paddingRight="8dp"
    android:singleLine="true"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TableLayout
    android:id="@+id/keyboard"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:divider="@drawable/keyboard_divider"
    android:orientation="vertical"
    android:showDividers="beginning|middle|end">

    <TableRow style="@style/keyboard_row">

        <TextView
            android:id="@+id/t9_key_1"
            style="@style/keyboard_number_button"
            android:text="@string/number_one" />

        <TextView
            android:id="@+id/t9_key_2"
            style="@style/keyboard_number_button"
            android:text="@string/number_two" />

        <TextView
            android:id="@+id/t9_key_3"
            style="@style/keyboard_number_button"
            android:text="@string/number_three" />
    </TableRow>

    <TableRow style="@style/keyboard_row">

        <TextView
            android:id="@+id/t9_key_4"
            style="@style/keyboard_number_button"
            android:text="@string/number_four" />

        <TextView
            android:id="@+id/t9_key_5"
            style="@style/keyboard_number_button"
            android:text="@string/number_five" />

        <TextView
            android:id="@+id/t9_key_6"
            style="@style/keyboard_number_button"
            android:text="@string/number_six" />
    </TableRow>

    <TableRow style="@style/keyboard_row">

        <TextView
            android:id="@+id/t9_key_7"
            style="@style/keyboard_number_button"
            android:text="@string/number_seven" />

        <TextView
            android:id="@+id/t9_key_8"
            style="@style/keyboard_number_button"
            android:text="@string/number_eight" />

        <TextView
            android:id="@+id/t9_key_9"
            style="@style/keyboard_number_button"
            android:text="@string/number_nine" />
    </TableRow>

    <TableRow style="@style/keyboard_row">

        <TextView
            android:id="@+id/t9_key_clear"
            style="@style/keyboard_button"
            android:text="@string/btn_clear"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/t9_key_0"
            style="@style/keyboard_number_button"
            android:text="@string/number_zero" />

        <TextView
            android:id="@+id/t9_key_backspace"
            style="@style/keyboard_button"
            android:text="@string/btn_backspace"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </TableRow>
</TableLayout>


首先,我的第一个疑问是关于标签的。我不知道我是否正确,但正如我所读到的,这些按钮用于当我们有许多按钮具有相同的onClick事件时,它们都使用相同的标记分组,因此我们不必多次编写相同的代码。我不明白的是,在哪里捕捉到对应于该按钮的数字,即:当我按下有1的按钮时,Android Studio如何知道应该放置1?我没有看到我所说的任何代码行,或者至少我不知道什么,这就是我来找你的原因。

我想这是一行

password_field.append((v as TextView).text)

我需要更多的代码或布局文件等。但从您发布的代码来看,这似乎是可以做到这一点的一行。

我用更多的类编辑了我的问题。我还认为这是处理文本集的那一行,但我不明白它是如何知道在文本视图中有1、2等等。上面这一行是从单击的按钮中获取文本并将其附加到密码字段。在布局文件中,您将每个TextView的文本设置为android:text=“@string/number\u one”。我假设该字符串引用Strings.xml文件中的字符“1”。因此,当您单击按钮时,它会获取按钮的文本,该文本位于strings.xml文件中。