android 加法程序

来源:互联网 发布:三国豪侠传单机版mac 编辑:程序博客网 时间:2024/06/08 06:24

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/one"    android:layout_width="fill_parent"     android:layout_height="wrap_content"     /> <TextView   android:id="@+id/tv"    android:layout_width="fill_parent"     android:layout_height="wrap_content"     />    <EditText     android:id="@+id/two"    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>

two.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"    > <TextView   android:id="@+id/twotv"    android:layout_width="fill_parent"     android:layout_height="wrap_content"     /></LinearLayout>

strings.xml

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="hello">Hello World, OneActivity!</string>    <string name="app_name">运算</string>    <string name="two_name">计算结果</string>    <string name="button">按下我</string>    <string name="textview">乘以</string>    <string name="exit">退出</string>    <string name="about">关于</string></resources>


 OneActivity

package com.hufan;import android.app.Activity;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.*;import android.os.Bundle;import android.widget.*;import android.content.*;public class OneActivity extends Activity implements OnClickListener{    EditText et1,et2;    TextView tv;    Button b;    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        et1=(EditText)findViewById(R.id.one);        et1.setText("4");        tv=(TextView)findViewById(R.id.tv);        tv.setText(R.string.textview);        et2=(EditText)findViewById(R.id.two);        et2.setText("5");        b=(Button)findViewById(R.id.button);        b.setOnClickListener(this);        b.setText(R.string.button);    }@Overridepublic boolean onCreateOptionsMenu(Menu menu) {menu.add(0,1,1,R.string.exit);menu.add(0,2,3,R.string.about);return super.onCreateOptionsMenu(menu);}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {if(item.getItemId()==1){this.finish();}return super.onOptionsItemSelected(item);}@Overridepublic void onClick(View v) {String oneStr=et1.getText().toString();String twoStr=et2.getText().toString();Intent in=new Intent();in.putExtra("one", oneStr);in.putExtra("two", twoStr);in.setClass(this, TwoActivity.class);this.startActivity(in);}}


TwoActivity
 

package com.hufan;import android.app.Activity;import android.view.View;import android.view.View.*;import android.os.Bundle;import android.widget.*;import android.content.*;public class TwoActivity extends Activity{TextView twotv;public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.two);                twotv=(TextView)findViewById(R.id.twotv);        Intent in=getIntent();        String one1=in.getStringExtra("one");        String two2=in.getStringExtra("two");        int a=Integer.parseInt(one1);        int b=Integer.parseInt(two2);        int c=a*b;        twotv.setText(c+"");}}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="com.hufan"      android:versionCode="1"      android:versionName="1.0">    <uses-sdk android:minSdkVersion="4" />    <application android:icon="@drawable/icon" android:label="@string/app_name">        <activity android:name=".OneActivity"                  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=".TwoActivity"                  android:label="@string/two_name">        </activity>    </application></manifest>

原创粉丝点击