在我的EditText
中,我需要用户仅输入5到10个十进制值的数据。
我如何实现这一目标?
如果用户输入的值小于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 :(得分:0)
解析他们输入的双精度数后,您可以执行一个简单的if语句来检查他们输入的值是否在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让用户知道他们输入的值不正确。
至于整个设置,如果将所有EditTexts放入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
}
有很多方法可以解决这个问题。其他一些选项可能更清晰,但我想向您展示一个相对容易理解的选项。