在文本框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:Landroid / content / res / TypedArray; .getChangingConfigurations()
答案 0 :(得分:0)
您应该只是将变量更改为正确的形式...... EditText和TextView之间是否存在混淆?
Button b1=(Button)findViewById(R.id.button1);
final TextView viewLove = (TextView) findViewById(R.id.textView1)
final EditText t1=(EditText)findViewById(R.id.editText1); //<----- here was your error!
final EditText t2=(EditText)findViewById(R.id.editText2);
// final EditText t3=(EditText)findViewById(R.id.editText3); <--- no need for this.
将xml中的TextView移动到EditText editText3
下方,并在onClick中移动:
int total=sum+sum2;
int lovepercentage=total%100;
viewLove.setText(""+lovepercentage);