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

时间:2015-06-29 18:10:53

标签: java android xml onclick android-edittext

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

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

当我进入三个editTexts并按“提交”时,按钮,应用程序停止并提交'按下按钮。似乎它在某种循环中 - 只是一个猜测。所以,tot msg并没有出现在我的屏幕上(即使我用@sunnyday提到的代码改变了我的代码)。该应用程序似乎停滞不前。它甚至不会回到以前的活动。

我刚刚开始使用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>

感谢您的时间:)

2 个答案:

答案 0 :(得分:0)

interned_range = [i for i in range(-1000, 1000) if i+0 is i]
print min(interned_range), max(interned_range)

必须:

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

Toast.makeText(Context,int,long)。在那里,int是资源ID。

答案 1 :(得分:0)

输入错误值后,您的验证会陷入无限循环。假设我为101输入people,那么只要您点击提交按钮,就会在此循环中结束。

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();
}