android学习笔记08(activity通信的一个实例--乘法计算器)

Activity03.java文件:

package tk.myactivity03;



import android.os.Bundle; 

import android.app.Activity; 

import android.content.Intent; 

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



    private EditText factorOne; 

    private EditText factorTwo; 

    private TextView symbol; 

    private Button calculate; 

    

    

    @Override 

    public void onCreate(Bundle savedInstanceState) { 

        super.onCreate(savedInstanceState); 

        setContentView(R.layout.activity03); 

        

        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 CalculateListrner()); 

        

    } 

    

    @Override 

    public boolean onCreatePanelMenu(int featureId, 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.onCreatePanelMenu(featureId, menu); 

    } 

    

    



    @Override 

    public boolean onOptionsItemSelected(MenuItem item) { 

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

            finish(); 

        } 

        return super.onOptionsItemSelected(item); 

    }







    class CalculateListrner implements OnClickListener{



        @Override 

        public void onClick(View v) { 

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

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

            

            Intent intent=new Intent(); 

            intent.putExtra("one", factorOneString); 

            intent.putExtra("two", factorTwoString); 

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

            

            Activity03.this.startActivity(intent); 

            

        } 

        

    

    } 

    



    @Override 

    public boolean onCreateOptionsMenu(Menu menu) { 

        getMenuInflater().inflate(R.menu.activity03, menu); 

        return true; 

    } 

}

ResultActivity.java文件:

package tk.myactivity03;



import android.app.Activity; 

import android.content.Intent; 

import android.os.Bundle; 

import android.widget.TextView;



public class ResultActivity  extends Activity{ 

    private TextView resultView=null; 

    @Override 

    public 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); 

        

    } 

}

activity03.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/factorOne" 

        android:layout_width="fill_parent" 

        android:layout_height="wrap_content" 

        ></EditText> 

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

        ></EditText> 

    <Button 

        android:id="@ id/calculate" 

        android:layout_width="fill_parent" 

        android:layout_height="wrap_content" 

        ></Button> 

</LinearLayout>

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>

manifest.xml配置文件:

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

    package="tk.myactivity03" 

    android:versionCode="1" 

    android:versionName="1.0" >



    <uses-sdk 

        android:minSdkVersion="8" 

        android:targetSdkVersion="15" />



    <application 

        android:icon="@drawable/ic_launcher" 

        android:label="@string/app_name" 

        android:theme="@style/AppTheme" > 

        <activity 

            android:name=".Activity03" 

            android:label="@string/title_activity_activity03" > 

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

            > 

        

        </activity> 

    </application>



</manifest>

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