没有得到答案在文本框中的爱计算器值的百分比3.当运行它的应用程序显示错误应用程序停止。以下是在mainactivity中输入的代码。没有得到答案在文本框中爱计算器值的百分比
EditText t1;
EditText t2;
EditText t3;
Button b1;
int sum=0;
int sum2=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1=(Button)findViewById(R.id.button1);
final TextView t1=(TextView)findViewById(R.id.textView1);
final EditText t2=(EditText)findViewById(R.id.editText2);
final EditText t3=(EditText)findViewById(R.id.editText3);
t1.setText("");
t2.setText("");
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String s1=t1.getText().toString();
String s2=t2.getText().toString();
for (int i = 0; i < t1.length(); i++) {
char ch= s1.charAt(i);
int ascii=ch;
sum=sum+ascii;
}
for (int i = 0; i <t2.length(); i++) {
char ch= s1.charAt(i);
int ascii=ch;
sum2=sum2+ascii;
}
int total=sum+sum2;
int lovepercentage=total%100;
t3.setText(""+lovepercentage);
}
});
}
XML代码是波纹管
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.lovecalculator.MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginLeft="24dp"
android:layout_marginTop="46dp"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="46dp"
android:ems="10" />
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText2"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"
android:ems="10" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText3"
android:layout_marginTop="18dp"
android:layout_toRightOf="@+id/textView1"
android:text="Button" />
logcat的消息 11-05 19:56:11.554:W/dalvikvm(678):VFY:无法解决虚拟方法406: LandText/content/res/TypedArray; .getChangingConfigurations()
是否打算T1是一个TextView,而不是一个EditText? 请发布您的logcat + xml。 – yennsarah
我已经更新了代码,上面提到的是那里。 – Vivek
只注意到你使用模运算符 - 这会给你余下的一个除法之后,而不是数字。只需使用除法并在浮点后剪切字符串。 – yennsarah