我叫什么干什么的我还是我不知道
分类: Java
2015-06-09 14:42:58
activity_main.xml
xmlns:tools=""
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="">
android:id="@+id/txtView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
android:id="@+id/myTxtB1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入一个数"
android:gravity="right"
/>
MainActivity.java
package com.mycalc.org;
import android.os.Bundle;
import android.R.string;
import android.app.Activity;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView txtV1=null;
private EditText txtB1=null;
private Button btnAdd=null;
private Button btnEql=null;
int op1=0;
int op2=0;
int result=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtV1 =(TextView)findViewById(R.id.txtView1);
txtB1=(EditText)findViewById(R.id.myTxtB1);
btnAdd=(Button)findViewById(R.id.myBtnAdd);
btnEql=(Button)findViewById(R.id.myBtnEql);
btnAdd.setOnClickListener(new MyClickListener());
btnEql.setOnClickListener(new MyClickListener());
}
@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;
}
class MyClickListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId()==btnAdd.getId()){
//txtV1.setText("你点了加号!");
op1=Integer.parseInt(txtB1.getText().toString());
txtB1.setText("");
}
if(v.getId()==btnEql.getId())
{
op2=Integer.parseInt(txtB1.getText().toString());
result=op1+op2;
txtB1.setText(Integer.toString(result));
txtB1.setSelection(txtB1.length());
}
}
}
}