代码应该通过两个edittext字段获取两个数字,并且在单击按钮时textview应该打印结果。那么,有谁能告诉我我做错了什么?
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button c = (Button) findViewById(R.id.sum_button);
c.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
EditText n1 = (EditText) findViewById(R.id.add_no1);
EditText n2 = (EditText) findViewById(R.id.add_no2);
TextView sum_result = (TextView)findViewById(R.id.sum_result);
int sum = 0,i=0,j=0;
i=Integer.getInteger(n1.getText().toString());
j=Integer.getInteger(n2.getText().toString());
sum=i+j;
sum_result.setText("the sum result is "+ sum);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
这是我的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:background="#000000"
android:orientation="horizontal"
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=".MainActivity" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/number_ip_layout"
android:layout_alignParentTop="true"
android:layout_marginTop="83dp"
android:orientation="horizontal"
android:visibility="visible"
android:weightSum="100" >
<EditText
android:id="@+id/add_no1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_weight="40"
android:background="#ffffff"
android:cursorVisible="true"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/add_no2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_weight="40"
android:background="#ffffff"
android:cursorVisible="true"
android:ems="10"
android:inputType="number" />
</LinearLayout>
<Button
android:id="@+id/sum_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/linearLayout1"
android:layout_centerHorizontal="true"
android:layout_marginTop="56dp"
android:background="#550000"
android:text="sum" />
<TextView
android:id="@+id/sum_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/sum_button"
android:layout_centerHorizontal="true"
android:layout_marginTop="128dp"
android:textSize="20sp"
android:singleLine="true"
android:text="" />
</RelativeLayout>
答案 0 :(得分:0)
它不是答案,但它有助于找到问题。
将onCreate函数更新为此。并查看onClick事件上的日志输出。让我知道日志输出。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button c = (Button) findViewById(R.id.sum_button);
final EditText n1 = (EditText) findViewById(R.id.add_no1);
final EditText n2 = (EditText) findViewById(R.id.add_no2);
final TextView sum_result = (TextView)findViewById(R.id.sum_result);
c.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
int sum = 0,i=0,j=0;
Log.i("n1 is:", n1.getText().toString()+" number");
Log.i("n2 is:", n2.getText().toString()+" number");
i=Integer.getInteger(n1.getText().toString());
j=Integer.getInteger(n2.getText().toString());
sum=i+j;
sum_result.setText("the sum result is "+ sum);
}
});
}