在我的Android应用程序中,我使用文本观察器添加编辑文本的值,其中每个编辑文本值的值被添加在一起并显示在文本视图中...例如,如果第一个编辑文本值是23和第二个编辑文本值为25它们都被加在一起并显示在文本视图中。 这个应用程序是关于命理学的..在第一个编辑文本中输入用户的第一个名字,在第二个用户输入中间名,在第三个编辑文本用户姓氏,每个字母都有自己的值1-9,以便相应的值将它们加在一起并使其达到1-9之间的值。对于所有三个编辑文本值,此过程重复,然后将所有三个编辑文本的值相加并使其为1-9并以文本形式显示 - 图。
我为此编写了代码,但它对于一个编辑文本工作得很好..但是当一起计算两个编辑文本某处发生错误任何一个如果理解错误请帮助..
MainActiviy
public class MainActivity extends Activity
{
EditText et1,et2,et3 ;
TextView tv1,tv2,tv3,tv4;
String name = "";
String name1 = "";
String name2 = "";
String hname = "";
int nameValue = 0;
int hnameValue = 0;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_main);
//TextView for displaying results
tv2 = (TextView) findViewById (R.id.tv7);
tv3 = (TextView) findViewById (R.id.tv9);
//tv2 = (TextView) findViewById (R.id.tv7);
//EditText for entering names
et1= (EditText) findViewById(R.id.editText1);
et2 = (EditText) findViewById(R.id.editText2);
et3 = (EditText) findViewById(R.id.editText3);
//Text change listner for first Name
et1.addTextChangedListener(new TextWatcher()
{
public void onTextChanged(CharSequence s, int start, int before, int count)
{
//s= (et1.getText().toString() + et2.getText().toString() + et3.getText().toString());
//showNameValue(s);
// showHeartNumber(s);
tv2.setText(addNumbers());
}
public void beforeTextChanged(CharSequence s, int p2, int p3, int p4)
{}
public void afterTextChanged(Editable s)
{
}
});
}
protected String addNumbers() {
// TODO Auto-generated method stub
int number1;
int number2;
int sum = 0;
if(et1.getText().toString() != "" && et1.getText().length() > 0) {
number1 = Integer.parseInt(et1.getText().toString());
} else {
number1 = 0;
}
if(et2.getText().toString() != "" && et2.getText().length() > 0) {
number2 = Integer.parseInt(et2.getText().toString());
} else {
number2 = 0;
}
name = Integer.toString(number1 + number2);
for (int i = 0; i < name.length(); i++)
{
sum += letterValue(name.charAt(i));
}
nameValue = sumDigits(sum);
//tvwNameSum.setText(String.valueOf(sum));
// tv2.setText(String.valueOf(nameValue));
return Integer.toString(nameValue);
}
private int sumDigits(int n) {
// TODO Auto-generated method stub
int sum = 0;
if (n == 11 || n == 22)
{
return n;
}
while (n > 0)
{
sum += n % 10;
n /= 10;
}
if (sum < 10)
{
return sum;
}
else
{
return sumDigits(sum);
}
}
private int letterValue(char c) {
// TODO Auto-generated method stub
if (c < 65 || c > 90)
{
return 0;
}
int v = (c - 64) % 9;
return v == 0 ? 9 : v;
}
}
logcat的
04-04 07:07:37.920: E/AndroidRuntime(3586): FATAL EXCEPTION: main
04-04 07:07:37.920: E/AndroidRuntime(3586): java.lang.NumberFormatException: Invalid int: "s"
04-04 07:07:37.920: E/AndroidRuntime(3586): at java.lang.Integer.invalidInt(Integer.java:138)
04-04 07:07:37.920: E/AndroidRuntime(3586): at java.lang.Integer.parse(Integer.java:375)
04-04 07:07:37.920: E/AndroidRuntime(3586): at java.lang.Integer.parseInt(Integer.java:366)
04-04 07:07:37.920: E/AndroidRuntime(3586): at java.lang.Integer.parseInt(Integer.java:332)
04-04 07:07:37.920: E/AndroidRuntime(3586): at com.example.newnumerology.MainActivity.addNumbers(MainActivity.java:122)
04-04 07:07:37.920: E/AndroidRuntime(3586): at com.example.newnumerology.MainActivity$1.onTextChanged(MainActivity.java:63)
04-04 07:07:37.920: E/AndroidRuntime(3586): at android.widget.TextView.sendOnTextChanged(TextView.java:7231)
04-04 07:07:37.920: E/AndroidRuntime(3586): at android.widget.TextView.handleTextChanged(TextView.java:7290)
04-04 07:07:37.920: E/AndroidRuntime(3586): at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:8880)
04-04 07:07:37.920: E/AndroidRuntime(3586): at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:962)
04-04 07:07:37.920: E/AndroidRuntime(3586): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:496)
04-04 07:07:37.920: E/AndroidRuntime(3586): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:435)
04-04 07:07:37.920: E/AndroidRuntime(3586): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:30)
04-04 07:07:37.920: E/AndroidRuntime(3586): at android.text.method.QwertyKeyListener.onKeyDown(QwertyKeyListener.java:222)
04-04 07:07:37.920: E/AndroidRuntime(3586): at android.text.method.TextKeyListener.onKeyDown(TextKeyListener.java:136)
04-04 07:07:37.920: E/AndroidRuntime(3586): at android.widget.TextView.doKeyDown(TextView.java:5385)
04-04 07:07:37.920: E/AndroidRuntime(3586): at android.widget.TextView.onKeyDown(TextView.java:5204)
04-04 07:07:37.920: E/AndroidRuntime(3586): at android.view.KeyEvent.dispatch(KeyEvent.java:2609)
04-04 07:07:37.920: E/AndroidRuntime(3586): at android.view.View.dispatchKeyEvent(View.java:7205)
04-04 07:07:37.920: E/AndroidRuntime(3586): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
04-04 07:07:37.920: E/AndroidRuntime(3586): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
04-04 07:07:37.920: E/AndroidRuntime(3586): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
04-04 07:07:37.920: E/AndroidRuntime(3586): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
04-04 07:07:37.920: E/AndroidRuntime(3586): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1920)
04-04 07:07:37.920: E/AndroidRuntime(3586): at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1395)
04-04 07:07:37.920: E/AndroidRuntime(3586): at android.app.Activity.dispatchKeyEvent(Activity.java:2370)
04-04 07:07:37.920: E/AndroidRuntime(3586): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1847)
04-04 07:07:37.920: E/AndroidRuntime(3586): at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3701)
04-04 07:07:37.920: E/AndroidRuntime(3586): at android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:3651)
04-04 07:07:37.920: E/AndroidRuntime(3586): at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:2818)
04-04 07:07:37.920: E/AndroidRuntime(3586): at android.os.Handler.dispatchMessage(Handler.java:99)
04-04 07:07:37.920: E/AndroidRuntime(3586): at android.os.Looper.loop(Looper.java:137)
04-04 07:07:37.920: E/AndroidRuntime(3586): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-04 07:07:37.920: E/AndroidRuntime(3586): at java.lang.reflect.Method.invokeNative(Native Method)
04-04 07:07:37.920: E/AndroidRuntime(3586): at java.lang.reflect.Method.invoke(Method.java:511)
04-04 07:07:37.920: E/AndroidRuntime(3586): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-04 07:07:37.920: E/AndroidRuntime(3586): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-04 07:07:37.920: E/AndroidRuntime(3586): at dalvik.system.NativeStart.main(Native Method)
04-04 07:07:38.310: D/dalvikvm(3586): GC_CONCURRENT freed 150K, 10% free 2692K/2972K, paused 59ms+89ms, total 542ms
答案 0 :(得分:2)
您正在将字符串“s”转换为int。这就是你得到这个例外的原因。
更好地限制EditText只输入整数
<EditText
android:inputType="number"
android:digits="0123456789."
/>
或
try{
if(et1.getText().toString() != "" && et1.getText().length() > 0) {
number1 = Integer.parseInt(et1.getText().toString());
} else {
number1 = 0;
}
if(et2.getText().toString() != "" && et2.getText().length() > 0) {
number2 = Integer.parseInt(et2.getText().toString());
} else {
number2 = 0;
}}
catch(NumberFormatException nfe)
{
// Handle the exception here.
}
答案 1 :(得分:1)
et1.addTextChangedListener(new TextWatcher()
{
public void onTextChanged(CharSequence s, int start, int before, int count)
{
showNameValue(s);
}
public void beforeTextChanged(CharSequence s, int p2, int p3, int p4)
{}
public void afterTextChanged(Editable s)
{
}
});
protected void showNameValue(CharSequence s)
{
// TODO Auto-generated method stub
int sum = 0;
name = s.toString().toUpperCase();
//name1 = s1.toString().toUpperCase();
for (int i = 0; i < name.length(); i++)
{
sum += letterValue(name.charAt(i));
}
nameValue = sumDigits(sum);
//tvwNameSum.setText(String.valueOf(sum));
tv2.setText(String.valueOf(nameValue));
}