2016-08-04 34 views
-1

在我的EditText我需要用戶輸入的數據只有5到10個十進制值。如何限制用戶在EditText框中僅輸入一系列值?

我該如何做到這一點?

如果用戶輸入小於5或大於10的值,我需要提示用戶5和10

package com.sabari.results; 

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

public class Calculator extends AppCompatActivity { 

    TextView tv1; 
    TextView tv2; 
    TextView tv3; 
    TextView tv4; 
    TextView tv5; 
    TextView tv6; 
    TextView tv7; 
    TextView tv8; 
    TextView tv10; 
    EditText et1; 
    EditText et2; 
    EditText et3; 
    EditText et4; 
    EditText et5; 
    EditText et6; 
    EditText et7; 
    EditText et8; 
    Button b1; 
    Button b2; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setTitle("CGPA Calculator"); 
     setContentView(R.layout.calculator); 

     tv1 = (TextView) findViewById(R.id.textView); 
     tv2 = (TextView) findViewById(R.id.textView2); 
     tv3 = (TextView) findViewById(R.id.textView3); 
     tv4 = (TextView) findViewById(R.id.textView4); 
     tv5 = (TextView) findViewById(R.id.textView5); 
     tv6 = (TextView) findViewById(R.id.textView6); 
     tv7 = (TextView) findViewById(R.id.textView7); 
     tv8 = (TextView) findViewById(R.id.textView8); 


     b1 = (Button) findViewById(R.id.button); 
     b2 = (Button)findViewById(R.id.button2); 

     b1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       et1 = (EditText) findViewById(R.id.editText); 
       et2 = (EditText) findViewById(R.id.editText2); 
       et3 = (EditText) findViewById(R.id.editText3); 
       et4 = (EditText) findViewById(R.id.editText4); 
       et5 = (EditText) findViewById(R.id.editText5); 
       et6 = (EditText) findViewById(R.id.editText6); 
       et7 = (EditText) findViewById(R.id.editText7); 
       et8 = (EditText) findViewById(R.id.editText8); 


       tv10 = (TextView) findViewById(R.id.textView10); 
       int n; 

       double no1=0, no2=0, no3=0, no4=0, no5=0, no6=0, no7=0, no8=0; 
       try { 
        no1 = Double.parseDouble(et1.getText().toString()); 
       }catch (NumberFormatException e){ 
        e.printStackTrace(); 
       } 
       try{ 
        no2 = Double.parseDouble(et2.getText().toString());} 
       catch (NumberFormatException e){ 
        e.printStackTrace(); 
       } 
       try { 
        no3 = Double.parseDouble(et3.getText().toString()); 
       }catch (NumberFormatException e){ 
        e.printStackTrace(); 
       } 
       try { 
        no4 = Double.parseDouble(et4.getText().toString()); 
       }catch (NumberFormatException e) 
       { 
        e.printStackTrace(); 
       } 
       try{ 
        no5 = Double.parseDouble(et5.getText().toString()); 
       }catch (NumberFormatException e){ 
        e.printStackTrace(); 
       } 
       try{ 
        no6 = Double.parseDouble(et6.getText().toString()); 
       }catch (NumberFormatException e){ 
        e.printStackTrace(); 
       } 
       try { 
        no7 = Double.parseDouble(et7.getText().toString()); 
       }catch (NumberFormatException e){ 
        e.printStackTrace(); 
       }try { 
        no8 = Double.parseDouble(et8.getText().toString()); 
       }catch (NumberFormatException e){ 
        e.printStackTrace(); 
       } 

       if (no1<=0) {  
        n = 1; 
       } else if (no3<=0||et3.equals("")) { 
        n = 2; 
       } else if (no4<=0||et4.equals("")) { 
        n = 3; 
       } else if (no5<=0||et5.equals("")) { 
        n = 4; 
       } else if (no6<=0||et6.equals("")) { 
        n = 5; 
       } else if (no7<=0||et7.equals("")) { 
        n = 6; 
       } else if (no8<=0||et8.equals("")) { 
        n = 7; 
       } else { 
        n = 8; 
       } 

       double res = 0; 

       if((no1<=10)&&(no2<=10)&&(no3<=10)&&(no4<=10)&&(no5<=10)&&(no6<=10)&&(no7<=10)&&(no8<=10)) { 
        res = (no1 + no2 + no3 + no4 + no5 + no6 + no7 + no8)/n; 
        tv10.setText("Your CGPA is : " + res); 
        Toast.makeText(Calculator.this, "Your CGPA is : " + res, Toast.LENGTH_LONG).show(); 
       } 
       else{ 
        Toast.makeText(Calculator.this,"Please enter a valid GPA ",Toast.LENGTH_LONG).show(); 
       } 
      } 
     }); 
     b2.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       et2.setText(""); 
       et3.setText(""); 
       et4.setText(""); 
       et5.setText(""); 
       et6.setText(""); 
       et7.setText(""); 
       et8.setText(""); 
       tv10.setText("Your CGPA is : "); 
       et1.setText(""); 
      } 
     }); 
    } 
} 
+0

感謝您發佈您的代碼,但您遇到的當前錯誤是什麼?你得到的輸出/結果是不是你所期望的?我們不能只爲你重寫你的代碼...... – Bek

回答

0

你解析他們進入雙後輸入之間的值,你可以做一個簡單的,如果檢查語句,如果他們輸入的值是5〜10:

或者

if (no1 < 5 || no1 > 10) 
{ 
    // error... prompt them to enter value between 5 and 10 
} 
,您可以檢查一個正確的值,像這樣:

if (no1 >= 5 && no1 <= 10) 
{ 
    // good value 
} 

而且,只允許用戶輸入一個浮點值,您可以將您的EditTexts的inputType屬性設置爲numberDecimal

<EditText 
    ... 
    android:inputType="numberDecimal" 
    ... /> 

您可以使用AlertDialog讓用戶知道他們輸入的值不正確。

對於您的整個設置,如果將所有EditText放入List /數組,並將所有的double值放入另一個List,則可以使代碼更清晰。 (關於這兩個值的字典也是一個不錯的選擇)。有了這個設置,你的8的try-catch塊大部分將被簡化爲:

for (int i = 0; i < editTexts.Length; i++) 
{ 
    try 
    { 
     nums[i] = Double.parseDouble(editTexts[i].getText().toString()); 
    } 
    catch (NumberFormatException e) 
    { 
     e.printStackTrace(); 
    } 
} 

您的如果值是5和10之間看起來相似,你可以有一個布爾值,指示是否檢查塊或者找不到無效值。當發現一個時,循環會中斷:

Boolean invalidFound = false; 
for (int i = 0; i < nums.Length && !invalidFound; i++) 
{ 
    invalidFound = (nums[i] < 5 || nums[i] > 10); 
} 

if (invalidFound) 
{ 
    //error 
} 
else 
{ 
    // good values 
} 

有很多方法可以設置它。其他一些選項可能更清潔,但我想告訴你一個相對容易理解的選項。

相關問題