找不到我无法从 editText 读取的原因

can't find reason why I can't read from editText

在下面的代码中,当按下 'submit' 按钮时,我试图从三个 editText 中获取值。得到String值后,我想将它们转换成int值,并检查它们是否在指定范围内。

但是我无法从 editText 获取值。我不确定这是问题所在,还是我的代码存在另一个更大的缺陷。

当我输入三个 editText 并按下 'submit' 按钮时,应用程序停止并按下 'submit' 按钮。好像它处于某种循环中 - 只是一个猜测。所以,toast msg 没有出现在我的屏幕上(即使在我用@sunnyday 提到的内容更改了我的代码之后)。该应用程序似乎停滞不前。它甚至不会回到以前的 activity.

我刚刚开始 Android,如有任何建议,我们将不胜感激。

Java代码:

package activities;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.example.chloe.myapplication.R;

public class PayActivity extends ActionBarActivity implements View.OnClickListener {
    Button submitButton, flip;
    Switch switchButton;
    EditText et_totalPeople, et_payPeople, et_amount;
    TextView tv_payPeople, tv_people;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_numofpeople);

        submitButton = (Button) findViewById(R.id.submitButton);
        switchButton = (Switch)findViewById(R.id.switchButton);
        et_totalPeople= (EditText) findViewById(R.id.et_totalPeople);
        et_payPeople= (EditText) findViewById(R.id.et_payPeople);
        et_amount = (EditText) findViewById(R.id.et_amount);

        switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked == true) {
                    et_payPeople.setEnabled(false);
                    et_payPeople.setClickable(false);
                } else {
                    et_payPeople.setEnabled(true);
                    et_payPeople.setClickable(true);
                }
            }
        });
        submitButton.setOnClickListener(this);
    }
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.submitButton:
                int people = Integer.parseInt(et_totalPeople.getText().toString());
                //tested with toast here.. didn't work, which means it can't even read from editText
                Toast.makeText(getApplicationContext(), people, Toast.LENGTH_LONG).show();
                while(people<1 || people>100) {
                    Toast.makeText(getApplicationContext(), "Enter values 1~100", Toast.LENGTH_LONG).show();
                    et_totalPeople.setSelectAllOnFocus(true);
                    people = Integer.parseInt(et_totalPeople.getText().toString());
                }
                if(switchButton.isChecked()==false) { /*Only certain people pay*/
                    int payer = Integer.parseInt(et_payPeople.getText().toString());
                    while(payer<1 || payer>100) {
                        Toast.makeText(getApplicationContext(), "Enter values 1~100", Toast.LENGTH_LONG).show();
                        et_payPeople.setSelectAllOnFocus(true);
                        payer = Integer.parseInt(et_payPeople.getText().toString());
                    }
                }
                int amount = Integer.parseInt(et_amount.getText().toString());
                while(amount<1 || amount>10000000) {
                    Toast.makeText(getApplicationContext(), "Enter values 1~10,000,000", Toast.LENGTH_LONG).show();
                    et_amount.setSelectAllOnFocus(true);
                    amount = Integer.parseInt(et_amount.getText().toString());
                }
                /*now all three values are valid, continue*/
            break;
        }
    }
}

xml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/restaurant2_lighter"
    android:layout_gravity="center">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:layout_marginLeft="10dp"
        android:layout_gravity="center"
        android:orientation="vertical">
        <View
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:background="#6effc118"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:text="Everyone pays:   "
                android:textSize="15dp"
                android:textColor="#ffffff"/>
                <Switch
                    android:id="@+id/switchButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textOff="NO"
                    android:textOn="YES"
                    android:textSize="15dp"/>
            </LinearLayout>
        <View
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:background="#6effc118"/>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Enter\nthe number\nof people"
                android:textSize="15dp"
                android:textColor="#ffffffff"
                android:layout_marginRight="40dp"/>
            <EditText
                android:id="@+id/et_totalPeople"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:hint="ex) 1~99  "
                android:layout_gravity="center"
                android:background="#4be3cc86"
                android:textColor="#ffffffff"
                android:padding="10dp"
                android:inputType="numberDecimal" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=" people"
                android:textColor="#ffffffff"
                android:layout_gravity="center"/>
            </LinearLayout>
        <View
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:background="#6effc118"/>
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="10dp">
                <TextView
                    android:id="@+id/tv_payPeople"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Enter\nthe number\nof people\nto pay"
                    android:textColor="#ffffffff"
                    android:layout_marginRight="40dp"
                    android:textSize="15dp"/>
                <EditText
                    android:id="@+id/et_payPeople"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:hint="ex) 1~99  "
                    android:layout_gravity="center"
                    android:background="#4be3cc86"
                    android:textColor="#ffffffff"
                    android:padding="10dp"
                    android:inputType="numberDecimal" />
                <TextView
                    android:id="@+id/tv_people"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text=" people"
                    android:textColor="#ffffffff"
                    android:layout_gravity="center"/>
                </LinearLayout>
        <View
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:background="#6effc118"/>
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="10dp">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Enter\nthe total\namount"
                    android:textSize="15dp"
                    android:textColor="#ffffffff"
                    android:layout_marginRight="40dp"/>
                <EditText
                    android:id="@+id/et_amount"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:hint="ex) 50000  "
                    android:layout_gravity="center"
                    android:background="#4be3cc86"
                    android:textColor="#ffffffff"
                    android:padding="10dp"
                    android:inputType="numberDecimal" />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text=" won"
                    android:textColor="#ffffffff"
                    android:layout_gravity="center"/>
                </LinearLayout>
        <View
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:background="#6effc118"/>
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center">
                <Button
                    android:id="@+id/submitButton"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="submit"
                    android:padding="10dp"
                    android:textSize="20dp"
                    android:layout_marginTop="30dp"/>
                </LinearLayout>
        </LinearLayout>
</LinearLayout>

感谢您的宝贵时间:)

Toast.makeText(getApplicationContext(), people, Toast.LENGTH_LONG).show();

必须是:

Toast.makeText(getApplicationContext(), String.valueOf(people), Toast.LENGTH_LONG).show();

Toast.makeText(上下文,整数,长)。在那里,int 是资源 ID。

一旦输入错误的值,您就会陷入验证的无限循环。假设我为 people 输入 101 然后你一点击提交按钮就进入了这个循环。

while(people<1 || people>100) {
    Toast.makeText(getApplicationContext(), "Enter values 1~100",   Toast.LENGTH_LONG).show();
    et_totalPeople.setSelectAllOnFocus(true);
    people = Integer.parseInt(et_totalPeople.getText().toString());
}

原因是,您的 while 循环在应用的 UI 线程 中运行,因此您不能输入不同的值,因为这是可能的编辑文本字段本身需要 UI 线程。

要解决此问题,请执行以下操作(在伪代码中):

onclick() {
    if (people < 1 || people > 100) {
        showToast();
        return;
    }
    if (other-validation-condition == false) {
        showOtherToast();
        return;
    }
    if (yet-another-validation-condition == false) {
        showYetAnotherToast();
        return;
    }
    // now, everything should be valid...
    continueDoingStuffWithValidatedValues();
}