Android学习笔记——menu

该工程的功能是实现两个数相乘,并在另外一个Activity中显示计算的结果

 

以下的代码是MainActivity.java中的代码

package com.example.menu;



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 MainActivity extends Activity {

    

    private EditText factorOne;

    private EditText factorTwo;

    private TextView symbol;

    private Button calculate;

    

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_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("乘以");

        //calculate.setText("计算");

        

        //为symbol和calculate设置显示的值

        symbol.setText(R.string.symbol);

        calculate.setText(R.string.calculate);

        //将监听器的对象绑定到按钮对象上面

        calculate.setOnClickListener(new CalculateListener());

    }

    

    

    @Override

    public boolean onCreateOptionsMenu(Menu menu) {

        // TODO Auto-generated method stub

        menu.add(0,1,1,R.string.exit);

        menu.add(0,2,2,R.string.about);

        return super.onCreateOptionsMenu(menu);

    }



    //当客户点击MENU按钮的时候,调用该方法

    @Override

    public boolean onOptionsItemSelected(MenuItem item) {

        // TODO Auto-generated method stub

        if(item.getItemId() == 1){

            finish();

        }

        return super.onOptionsItemSelected(item);

    }





    class CalculateListener implements OnClickListener{



        @Override

        public void onClick(View v) {

            // TODO Auto-generated method stub

            //取得两个EditText控件的值

            String factorOneStr = factorOne.getText().toString();

            String factorTwoStr = factorTwo.getText().toString();

            //将这两个值存放到Intent对象当中

            Intent intent = new Intent();

            intent.putExtra("one",factorOneStr);

            intent.putExtra("two",factorTwoStr);

            intent.setClass(MainActivity.this,ResultActivity.class );

            //使用这个Intent对象来启动ResultActivity

            MainActivity.this.startActivity(intent);

        }

        

        

    }

}

 

以下的代码是ResultActivity.java中的代码

package com.example.menu;



import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.widget.TextView;



public class ResultActivity extends Activity{

    

    private TextView resultView;

    

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_result);

        resultView = (TextView)findViewById(R.id.result);

        //得到Intent对象当中的值

        Intent intent = getIntent();

        String factorOneStr = intent.getStringExtra("one");

        String factorTwoStr = intent.getStringExtra("two");

        int factorOneInt = Integer.parseInt(factorOneStr);

        int factorTwoInt = Integer.parseInt(factorTwoStr);

        //计算两个值的积

        int result = factorOneInt * factorTwoInt;

        resultView.setText(result + "");

    }

}

 

以下的代码是activity_main.xml中的代码

<?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/factorOne"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

      />  

        

    <TextView

        android:id="@+id/symbol"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        />



     <EditText

        android:id="@+id/factorTwo"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

      /> 

      

     <Button

         android:id="@+id/calculate"

         android:layout_width="fill_parent"

         android:layout_height="wrap_content"

         />

    

</LinearLayout>

 

以下的代码是activity_result.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/result"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        />

        



</LinearLayout>

 

以下的代码是string.xml中的代码

<?xml version="1.0" encoding="utf-8"?>

<resources>



    <string name="app_name">menu</string>

    <string name="hello_world">Hello world!</string>

    <string name="resultLabel">result</string>

    <string name="symbol">乘法</string>

    <string name="calculate">计算</string>

    <string name="exit">退出</string>

    <string name="about">关于</string>

</resources>

 

以下的代码是AndroidManifest.xml中的代码

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.example.menu"

    android:versionCode="1"

    android:versionName="1.0" >



    <uses-sdk

        android:minSdkVersion="8"

        android:targetSdkVersion="21" />



    <application

        android:allowBackup="true"

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:name=".MainActivity"

            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">

        </activity>

    </application>



</manifest>

 

你可能感兴趣的:(Android学习)