我这里有问题,我的java代码是
int num = Integer.parseInt(mark.getText().toString());
我的xml代码是
android. InputType="number"
但我仍然收到错误说"invalid int ="""
任何人都可以帮助我..我现在真的需要帮助。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_adding_remove);
parentLinearLayout = (LinearLayout) findViewById(R.id.parent_linear_layout);
final EditText mark = (EditText)findViewById(R.id.etMark);
EditText j = (EditText)findViewById(R.id.etJam);
TextView status = (TextView)findViewById(R.id.tVGred);
TextView pn = (TextView)findViewById(R.id.tvPnm);
double pointer;
String grade;
int num = Integer.parseInt(mark.getText().toString());
if (num >= 90 && num <= 100) {
grade = "A+";
pointer = 4.00;
status.setText(grade);
}
else if (num >= 80 && num <= 89) {
grade = "A";
pointer = 4.00;
status.setText(grade);
}
else if (num >= 75 && num <= 79) {
grade = "A-";
pointer = 3.67;
status.setText(grade);
}
else if (num >= 70 && num <= 74) {
grade = "B+";
pointer = 3.33;
status.setText(grade);
}
else if (num >= 65 && num <= 69) {
grade = "B";
pointer = 3.00;
status.setText(grade);
}
else if (num >= 60 && num <= 64) {
grade = "B-";
pointer = 2.67;
status.setText(grade);
}
else if (num >= 55 && num <= 59) {
grade = "C+";
pointer = 2.33;
status.setText(grade);
}
else if (num >= 50 && num <= 54) {
grade = "C";
pointer = 2.00;
status.setText(grade);
}
else if (num >= 47 && num <= 49) {
grade = "C-";
pointer = 1.67;
status.setText(grade);
}
else if (num >= 44 && num <= 46) {
grade = "D+";
pointer = 1.33;
status.setText(grade);
}
else if (num >= 40 && num <= 43) {
grade = "D";
pointer = 1.00;
status.setText(grade);
}
else
{
grade = "F";
pointer = 0.00;
status.setText(grade);
}
}
答案 0 :(得分:0)
问题在于
int num = Integer.parseInt(mark.getText().toString());
您的代码是尝试从EDITTEXT获取文本但是包含&#34;&#34;当ActiVity创建时。所以invalid Int
的Activity Throw错误因此你需要添加任何Button So Activity Get String当Button按下Trigger时。但是,如果您不想使用任何按钮使用Enter键作为触发器来更改焦点并获得String Fri EditText,就像这样。
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
public class Main2Activity extends AppCompatActivity {
int num =0;
EditText mark;
EditText j;
TextView status,pn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
mark = (EditText)findViewById(R.id.edt);
j = (EditText)findViewById(R.id.jmark);
status = (TextView)findViewById(R.id.txt);
pn = (TextView)findViewById(R.id.txt1);
final double[] pointer = new double[1];
final String[] grade = new String[1];
mark.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(KeyEvent.KEYCODE_ENTER == keyCode) {
j.requestFocus();
if(mark.getText().toString()!="")
num= Integer.parseInt(mark.getText().toString());
if (num >= 90 && num <= 100) {
grade[0] = "A+";
pointer[0] = 4.00;
status.setText(grade[0]);
}
else if (num >= 80 && num <= 89) {
grade[0] = "A";
pointer[0] = 4.00;
status.setText(grade[0]);
}
else if (num >= 75 && num <= 79) {
grade[0] = "A-";
pointer[0] = 3.67;
status.setText(grade[0]);
}
else if (num >= 70 && num <= 74) {
grade[0] = "B+";
pointer[0] = 3.33;
status.setText(grade[0]);
}
else if (num >= 65 && num <= 69) {
grade[0] = "B";
pointer[0] = 3.00;
status.setText(grade[0]);
}
else if (num >= 60 && num <= 64) {
grade[0] = "B-";
pointer[0] = 2.67;
status.setText(grade[0]);
}
else if (num >= 55 && num <= 59) {
grade[0] = "C+";
pointer[0] = 2.33;
status.setText(grade[0]);
}
else if (num >= 50 && num <= 54) {
grade[0] = "C";
pointer[0] = 2.00;
status.setText(grade[0]);
}
else if (num >= 47 && num <= 49) {
grade[0] = "C-";
pointer[0] = 1.67;
status.setText(grade[0]);
}
else if (num >= 44 && num <= 46) {
grade[0] = "D+";
pointer[0] = 1.33;
status.setText(grade[0]);
}
else if (num >= 40 && num <= 43) {
grade[0] = "D";
pointer[0] = 1.00;
status.setText(grade[0]);
}
else
{
grade[0] = "F";
pointer[0] = 0.00;
status.setText(grade[0]);
}
return true;
}
else
{
return false;
}
}
});
}
}
确定你添加了android:singleLine="true"
你的xml文件
答案 1 :(得分:0)
use this
<EditText
android:id="@+id/login_text_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:digits="0123456789"
android:maxLength="10" />
答案 2 :(得分:0)
try this
int num = toInteger(mark.getText().toString());
public static Integer toInteger(Object value) {
if (value instanceof Integer) {
return (Integer) value;
} else if (value instanceof Number) {
return ((Number) value).intValue();
} else if (value instanceof String) {
try {
return (int) Double.parseDouble((String) value);
} catch (NumberFormatException ignored) {
}
}
return null;
}