在下面的代码中,当'submit'按钮被按下时,我试图从三个editTexts中获取值。获取字符串值后,我想将它们转换为int值并检查它们是否在指定范围内。找不到原因为什么我无法从editText读取
但是我无法从editText获取值。我不确定这是否是问题,或者我的代码是否存在另一个更大的缺陷。
当我输入三个editText并按下'submit'按钮时,应用程序停止并按下'submit'按钮。看起来好像它在某种循环中 - 只是一个猜测。所以,我的屏幕上没有出现祝酒信息(即使我用@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>
感谢您的时间:)
哇谢谢!这解决了我的问题。我对线索了解不多,但我必须仔细研究。谢谢:) – awefawe
是的,在Android开发中,您总是不得不考虑在主线程(又称UI线程)中执行任何更长时间的任务,因为这会冻结UI。对于更持久的东西,有AsyncTask,Handlers等。 – Ridcully