2015-06-29 52 views
1

在下面的代碼中,當'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> 

感謝您的時間:)

回答

0

只要輸入了錯誤的值,您就會陷入無限循環。假設我爲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(); 
} 
+0

哇謝謝!這解決了我的問題。我對線索瞭解不多,但我必須仔細研究。謝謝:) – awefawe

+0

是的,在Android開發中,您總是不得不考慮在主線程(又稱UI線程)中執行任何更長時間的任務,因爲這會凍結UI。對於更持久的東西,有AsyncTask,Handlers等。 – Ridcully

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

必須是:

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

Toast.makeText(Context,int,long)。在那裏,int是資源ID。

+0

我明白了!謝謝。但我的問題是,當我輸入三個editText並按下'submit'按鈕時,應用程序停止並按下'submit'按鈕。看起來好像它在某種循環中 - 只是一個猜測。所以,即使我改變了這一點,我的屏幕上仍然沒有出現祝詞。該應用程序似乎停滯不前。它甚至不會回到以前的活動。 – awefawe

相關問題