Android起步–簡單的乘法 – Android移動開發技術文章_手機開發 Android移動開發教學課程

在網上看瞭android的相關視頻,本人也試著寫瞭一些代碼。現在我在這裡貼出來,用來見證我學習android的歷程。
先佈局:
main.xml
Java代碼 
<?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/num1" 
    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"  
    android:text="@string/hello" 
    /> 
<EditText 
    android:id="@+id/num2" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
/> 
<Button 
    android:id="@+id/buttonresult" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
/> 
</LinearLayout> 

result.xml
Java代碼 
<?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> 


strings.xml
Java代碼 
<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <string name="hello">Hello World, Activity03!</string> 
    <string name="app_name">Activity03</string> 
    <string name="symbol">乘以</string> 
    <string name="buttonresult">計算</string> 
    <string name="resultLable">result</string> 
    <string name="exit">退出</string> 
    <string name="about">關於</string> 
    <style name="Text"> 
        <item name="android:textSize">18sp</item> 
    </style> 
    <style name="SpecialText" parent="@style/Text"> 
        <item name="android:textSize">18sp</item> 
        <item name="android:textColor">#008</item> 
    </style> 
</resources> 

AndroidManifest.xml
Java代碼 
<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
      package="org.hs" 
      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=".Activity03" 
                  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/resultLable"></activity> 
    </application> 
</manifest> 


Activity03.java
Java代碼 
package org.hs; 
 
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 Activity03 extends Activity { 
    /** Called when the activity is first created. */ 
    private EditText num1; 
    private EditText num2; 
    private TextView symbol; 
    private Button buttonresult; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        num1 = (EditText) findViewById(R.id.num1); 
        num2 = (EditText) findViewById(R.id.num2); 
        symbol = (TextView) findViewById(R.id.symbol); 
        buttonresult = (Button) findViewById(R.id.buttonresult); 
         
//        symbol.setText("乘以"); 
//        buttonresult.setText("計算"); 
        symbol.setText(R.string.symbol); 
        buttonresult.setText(R.string.buttonresult); 
        buttonresult.setOnClickListener(new CalculateListener()); 
    } 
     
     
    @Override 
    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); 
    } 
     
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
        //菜單響應 
        super.onOptionsItemSelected(item); 
        switch (item.getItemId()) { 
        case 1: 
             
            break; 
        case 2: 
            finish(); 
            break; 
        default: 
            break; 
        } 
        return true; 
    } 
     
    class CalculateListener implements OnClickListener{ 
 
        public void onClick(View v) { 
            String num1Str = num1.getText().toString();//獲取值 
            String num2Str = num2.getText().toString(); 
            //將兩個值存放在Intent對象中 
            Intent intent=new Intent(); 
            intent.putExtra("one", num1Str); 
            intent.putExtra("two", num2Str); 
            intent.setClass(Activity03.this,ResultActivity.class ); 
            //使用這個Intent對象來啟動ResultActivity 
            Activity03.this.startActivity(intent); 
        } 
         
    } 

ResultActivity.java
Java代碼 
package org.hs; 
 
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) { 
        // TODO Auto-generated method stub 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.result); 
        resultView = (TextView) findViewById(R.id.result); 
        Intent intent = getIntent(); 
        String num1Str = intent.getStringExtra("one"); 
        String num2Str = intent.getStringExtra("two"); 
        int num1 = Integer.parseInt(num1Str); 
        int num2 = Integer.parseInt(num2Str); 
        int result = num1*num2; 
        resultView.setText(result+"");//setText隻接受字符串 
    } 

 

作者“如果有夢”
 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。