用TextView和EditText写一个简单的计算器,能实现加减乘除的操作
package com.ko8e;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Activity01 extends Activity {
/** Called when the activity is first created. */
private TextView view = null;
private EditText edit1 = null;
private EditText edit2 = null;
private Button button = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
view = (TextView) findViewById(R.id.view);
view.setText(R.string.view);
edit1 = (EditText) findViewById(R.id.edit1);
edit2 = (EditText) findViewById(R.id.edit2);
button = (Button) findViewById(R.id.button);
button.setText(R.string.caculate);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
String s1 = edit1.getText().toString();
String s2 = edit2.getText().toString();
Intent intent = new Intent();
intent.putExtra("s1", s1);
intent.putExtra("s2", s2);
intent.setClass(Activity01.this, ResultActivity.class);
Activity01.this.startActivity(intent);
}
});
}
}
计算得到的值放在跳转的Activity
package com.ko8e;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class ResultActivity extends Activity {
/** Called when the activity is first created. */
private TextView view2 = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
view2 = (TextView) findViewById(R.id.resultview);
Intent intent = getIntent();
String firstValueStr = intent.getStringExtra("s1");
String secondValueStr = intent.getStringExtra("s2");
int n1 = Integer.parseInt(firstValueStr);
int n2 = Integer.parseInt(secondValueStr);
int resultValue = (n1 * n2);
view2.setText(resultValue + "");
}
}
在layout文件中再添加一个xml(略)。
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText
android:id="@+id/edit1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/edit2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
记得在manifest.xml中把第二个Activity添加进去:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ko8e"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Activity01"
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=".ResultActivity"
android:label="@string/app_name"
/>
</application>
<uses-sdk android:minSdkVersion="4" />
</manifest>