2014-02-26 192 views
1

我正在製作一個數學應用程序。我有兩個數組的兩個數組。我讓應用程序從每個數組中選擇一個隨機數。該應用程序在TextView中顯示兩個數字。它要求用戶輸入一個EditText這個數字的加法是什麼。然後,當用戶按下「提交」時,應用程序會告訴用戶他們是對還是錯。檢查EditText是否爲空

該應用程序的工作原理,但我有一個主要問題。如果用戶沒有在EditText中輸入任何內容並按下「提交」,則應用程序部隊將關閉。我看了其他問題,詢問如何檢查EditText是否爲空。我嘗試了不同的方法,但它仍然不起作用。

public class MainActivity extends Activity { 
Button submitb, startb, idkb; 
EditText et; 
TextView tv, rig, wron; 

int arrone[] = { 24, 54, 78, 96, 48, 65, 32, 45, 95, 13, 41, 55, 33, 22, 
     71, 5, 22, 17, 13, 29, 63, 72, 14, 81, 7 }; // One Set of Numbers// 
int arrtwo[] = { 121, 132, 147, 79, 82, 723, 324, 751, 423, 454, 448, 
     524, 512, 852, 845, 485, 775, 186, 99 }; // Another set of Numbers// 
int random1, random2, add; 
int wrong = 0; 
int right = 0; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    list(); 
    startb.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      int num = (int) (Math.random() * 25); 
      int mun = (int) (Math.random() * 19); 
      random1 = arrone[num]; 
      random2 = arrtwo[mun]; 
      String yu = (random1 + " + " + random2 + " ="); 
      tv.setText(yu); 
      et.setHint(""); 
      idkb.setVisibility(0); 
      startb.setText("Next Question"); 

     } 
    }); 

    startb.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      int bread2 = 0; 
      if (bread2 == 0) { 
       Toast.makeText(getApplicationContext(), 
         "You didn't answer the question!", 
         Toast.LENGTH_LONG).show(); 

      } 

      int swag = random1 + random2; 
      String bread = et.getText().toString(); 
      bread2 = Integer.parseInt(bread); 

      if (bread2 == swag) { 
       startb.setText("Next Question"); 
       tv.setText("Correct"); 
       et.setText(""); 
       et.setHint("Click Next Question"); 
       right++; 
       rig.setText("Right:" + right); 
       rig.setTextColor(Color.parseColor("#14df11")); 
       bread2 = 0; 
      } 

      else if (bread2 == 0) { 
       tv.setText("Wrong, the correct answer is " + swag + "."); 

       startb.setText("Next Question"); 
       et.setText(""); 
       tv.setHint("Click Next Question"); 
       wrong++; 
       wron.setText("Wrong:" + wrong); 
       wron.setTextColor(Color.parseColor("#ff0000")); 
       bread2 = 0; 
      } 

      else { 
       tv.setText("Wrong, the correct answer is " + swag + "."); 

       startb.setText("Next Question"); 
       et.setText(""); 
       et.setHint("Click Next Question"); 
       wrong++; 
       wron.setText("Wrong:" + wrong); 
       wron.setTextColor(Color.parseColor("#ff0000")); 
       bread2 = 0; 

      } 

     } 

    }); 

    idkb.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      int swag = random1 + random2; 

      tv.setText("The Correct Answer is " + swag + "."); 

      et.setText(""); 
      tv.setHint("Click Next Question"); 
      wrong++; 
      wron.setText("Wrong:" + wrong); 
      wron.setTextColor(Color.parseColor("#ff0000")); 

     } 
    }); 

} 

private void list() { 
    submitb = (Button) findViewById(R.id.b1); 
    startb = (Button) findViewById(R.id.b2); 
    et = (EditText) findViewById(R.id.et1); 
    tv = (TextView) findViewById(R.id.tv1); 
    rig = (TextView) findViewById(R.id.rtv); 
    wron = (TextView) findViewById(R.id.wtv); 
    idkb = (Button) findViewById(R.id.idk); 

} 
+0

包括從接近 – Daenyth

+0

堆棧跟蹤你有沒有添加了任何邏輯來檢查空或空狀態。發佈,以及。 –

+0

沒有人使用'trim()'從末端移除空格???? –

回答

0

您需要在et使用findViewById之前得到它的價值。

順便說一下,你將bread2的值設置爲0,並在下一行驗證它是否爲0,我想這只是一個測試,不是嗎?

@Override 
    public void onClick(View v) { 

     int bread2 = 0; 
     if (bread2 == 0) { 
      Toast.makeText(getApplicationContext(), 
        "You didn't answer the question!", 
        Toast.LENGTH_LONG).show(); 

     } 

     int swag = random1 + random2; 

     et = findViewById(R.id.yourEditText); 

     String bread = et.getText().toString(); 
     bread2 = Integer.parseInt(bread); 

     //... 

希望它有幫助!

1

不知道我在正確認識你,但它應該是很容易的:

et = (EditText) findViewById(R.id.et1); 
String bread = et.getText().toString(); 
if (bread.length() == 0){ 
    // raise error dialogue 
} else { 
    // continue to check the parsed integer 
} 

這應該所有點擊監聽器內進行。

0

我認爲這很好的做法來檢查文本長度,比如該功能可以幫助你..

public boolean isEditTextEmpty(EditText input){ 
    if(input.getText().length() == 0) 
     return true; 
    else 
     return false; 
    } 
2

幾點考慮:

  • 你在STARTB設置OnClickListener兩次,這只是一個錯字嗎?
  • 在第二的onClick()
  • ,你有一個條件:

    int bread2 = 0; 
    if (bread2 == 0) { 
        //rest of code 
    } 
    

這個條件總是滿足,是你想要的嗎?

  • 您可以檢查是否有什麼使用輸入下列要求:

    String bread = et.getText().toString(); 
    
    // check if there is any text entered by user 
    if (bread.length() > 0) { 
        bread2 = Integer.parseInt(bread); 
    
        if (bread2 == swag) { 
         startb.setText("Next Question"); 
    
         //the rest of the code 
    } 
    
0
if (et.getText().length == 0 || et.gettext == null){ 
     // do nothing 
    } else { 
     // do whatever here 
    } 

這應該工作正常

+0

你應該首先檢查** null **否則你會得到一個NPE。 – CSchulz