-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("");
}
});
}
}
感謝您發佈您的代碼,但您遇到的當前錯誤是什麼?你得到的輸出/結果是不是你所期望的?我們不能只爲你重寫你的代碼...... – Bek