Android学习历程-Android常用控件(简单计算器)
两个布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <EditText android:id="@+id/numOne" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="text" /> <TextView android:id="@+id/symbol" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <EditText android:id="@+id/numTwo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="text" /> <Button android:id="@+id/calculate" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/resultTextView" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </LinearLayout>
两个Activity:
package com.may.activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
/**
*
*/
public class Activity04 extends Activity {
private EditText numOne = null;
private EditText numTwo = null;
private TextView symbol = null;
private Button calculate = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity04);
numOne=(EditText)findViewById(R.id.numOne);
numTwo=(EditText)findViewById(R.id.numTwo);
symbol=(TextView)findViewById(R.id.symbol);
calculate=(Button)findViewById(R.id.calculate);
//为symbol
symbol.setText("乘以");
calculate.setText("计算");
calculate.setOnClickListener(new CalculateListener());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 1, 1, R.string.exit);
menu.add(0, 2, 2, R.string.about);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==1){
finish();
}else if(item.getItemId()==2){
}
return super.onOptionsItemSelected(item);
}
class CalculateListener implements OnClickListener{
@Override
public void onClick(View v) {
String numOneStr = numOne.getText().toString().trim();
String numTwoStr = numTwo.getText().toString().trim();
Intent inten = new Intent();
inten.putExtra("one", numOneStr);
inten.putExtra("two", numTwoStr);
inten.setClass(Activity04.this, Activity05.class);
Activity04.this.startActivity(inten);
}
}
}
package com.may.activity; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; public class Activity05 extends Activity { private TextView resultTextView = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity05); resultTextView = (TextView) findViewById(R.id.resultTextView); Intent intent = getIntent(); int numOne = Integer.valueOf(intent.getStringExtra("one")); int numTwo = Integer.valueOf(intent.getStringExtra("two")); int resultValue = numOne * numTwo; resultTextView.setText(resultValue+""); } }
在AndroidMainfest添加:
<activity android:name="com.may.activity.Activity04" android:label="sub" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.may.activity.Activity05" android:label="result" > </activity>