package ahu.edu;
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 Activity03Activity extends Activity {
private EditText factorOne;
private EditText factorTwo;
private TextView symbol;
private Button calculate;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
factorOne = (EditText)findViewById(R.id.factorOne);
factorTwo = (EditText)findViewById(R.id.factorTwo);
symbol = (TextView)findViewById(R.id.symbol);
calculate = (Button)findViewById(R.id.calculate);
symbol.setText(R.string.symbol);
calculate.setText(R.string.calculate);
calculate.setOnClickListener(new CalcualteListener());
}
class CalcualteListener implements OnClickListener {
public void onClick(View v) {
String factorOneStr = factorOne.getText().toString();
String factorTwoStr = factorTwo.getText().toString();
Intent intent = new Intent();
intent.putExtra("one", factorOneStr);
intent.putExtra("two", factorTwoStr);
intent.setClass(Activity03Activity.this,ResualActivity.class);
Activity03Activity.this.startActivity(intent);
}
}
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);
}
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == 1) {
//finish(); //----比较柔和的方式
System.exit(0); //---残酷的方式
}
return super.onOptionsItemSelected(item);
}
}
package ahu.edu;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class ResualActivity extends Activity{
private TextView resultView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
resultView = (TextView)findViewById(R.id.result);
Intent intent = getIntent();
String factorOneString = intent.getStringExtra("one");
String factorTwoString = intent.getStringExtra("two");
int factorOneInt = Integer.parseInt(factorOneString);
int factorTwoInt = Integer.parseInt(factorTwoString);
int result = factorOneInt * factorTwoInt;
resultView.setText(result + "");
}
}
<?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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<EditText
android:id="@+id/factorOne"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/message"
android:inputType="none"/>
<TextView
android:id="@+id/symbol"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""/>
<EditText
android:id="@+id/factorTwo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/message"
android:inputType="none"/>
<Button
android:id="@+id/calculate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ahu.edu"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".Activity03Activity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ResualActivity"
android:label="@string/app_name">
</activity>
</application>
</manifest>