Android Training

從Android Training開始學起

參考網址: http://developer.android.com/training/basics/firstapp/starting-activity.html

第一個範例

功能:

建立一個應用程式,在該程式中的訊息框輸入訊息A後,按下Send 按鈕則可切換至另一畫面,此畫面將訊息A重新顯示出來。



應用:

1. 練習UI元件配置,EditText, Button元件的應用以及Layout的基礎。
2. 運用Intent來做兩個Activity之間切換,並且傳遞訊息。

程式碼:

1. activity_input_box.xml:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/TableLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".InputBoxActivity" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/ID" />

        <EditText
            android:id="@+id/EditTMessage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="text" >

            <requestFocus />
        </EditText>

        <Button
            android:id="@+id/BtnSend"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="SendBtn1_OnClick"
            android:text="@string/SendBtn1" />

    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textNumber"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Number" />

        <EditText
            android:id="@+id/EditTNumber"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="number" />

    </TableRow>

</TableLayout>

2. activity_send_result.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SendResult" >

    <TextView
        android:id="@+id/Label1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="22dp"
        android:text="@string/ID" />

    <TextView
        android:id="@+id/ShowM1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/Label1"
        android:layout_alignBottom="@+id/Label1"
        android:layout_marginLeft="16dp"
        android:layout_toRightOf="@+id/Label1"
        android:text="&quot;&quot;" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/Label1"
        android:layout_marginTop="15dp"
        android:text="@string/Number" />

    <TextView
        android:id="@+id/textShowNumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView1"
        android:layout_alignBottom="@+id/textView1"
        android:layout_toRightOf="@+id/ShowM1"
        android:text="&quot; &quot;" />

</RelativeLayout>

3. InputBoxActivity.java


package com.example.inputbox;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

public class InputBoxActivity extends Activity {

    public final static String EM1 = "ID";
    public final static String EM2 = "NUMBER";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_input_box);
   
    }
  
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_input_box, menu);
        return true;
    }

    public void SendBtn1_OnClick(View v1){
       
        Intent i1 =  new Intent(this, SendResult.class);
        EditText idinput = (EditText) findViewById(R.id.EditTMessage);
        EditText number = (EditText) findViewById(R.id.EditTNumber);
        String s1 = idinput.getText().toString();
        String s2 = number.getText().toString();
        Log.d("Test", "prepare to put s1");
        i1.putExtra(EM1, s1);
        Log.d("Test", "prepare to put s2");
        i1.putExtra(EM2, s2);
        Log.d("Test", "Put Ok");
        startActivity(i1);
    }
}


4. SendResult.java

package com.example.inputbox;

import android.os.Bundle;
import android.app.Activity;
import android.widget.TextView;

public class SendResult extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_result);
        TextView M1 = (TextView) findViewById(R.id.ShowM1);
        TextView M2 = (TextView) findViewById(R.id.textShowNumber);
        String g1 = getIntent().getStringExtra(InputBoxActivity.EM1);
        String g2 = getIntent().getStringExtra(InputBoxActivity.EM2);
        M1.setText(g1);
        M2.setText(g2);
    }
}

5. InputBoxManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.inputbox"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.inputbox.InputBoxActivity"
            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="com.example.inputbox.SendResult"
            android:label="@string/title_activity_send_result" >
        </activity>
    </application>

</manifest>

圖示:


起始畫面

按下按鈕後的新顯示畫面

問題:

1. 增加SendResult.java檔時,系統會自動生成onCreateOptionsMenu()函式,若不去除掉模擬器運行中途會出錯。

發想:

1. 利用Bundle來做數據傳輸的實驗。

留言