7、案例--短信助手

7、案例–短信助手(跳轉到下一個界面並返回這個界面的數據到原來的界面

如圖:
這裏寫圖片描述

代碼體現:

Layout界面代碼

第一個是主頁面的界面代碼:

<LinearLayout 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"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <EditText
            android:layout_marginTop="15dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="請輸入收信人" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:onClick="selectcontact"
            android:text="+" />
    </RelativeLayout>

    <EditText
        android:id="@+id/et_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="請輸入短信內容"
        android:minLines="7" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="selectsms"
        android:text="選擇短信模板" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="發送" />

</LinearLayout>

第2個是短信模板界面和號碼選擇界面的模板界面的代碼:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <ListView 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/lv"
        ></ListView>

</LinearLayout>

activity界面代碼

第一個是主頁面界面所對應的activity代碼:

package com.itheima.smshelper;

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

public class MainActivity extends Activity {
    EditText et_content; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        et_content =    (EditText) findViewById(R.id.et_content);
    /*  Intent  intent = getIntent();
        String content = intent.getStringExtra("content");
        et_content.setText(content);*/
    }



    public void selectsms(View v){

        Intent intent = new Intent(this , SmsActivity.class);

//      startActivity(intent);
        //1. 啓動界面,並且等待結果的返回。
        startActivityForResult(intent, 1);
    }

    public void selectcontact(View v){
        Intent intent = new Intent(this , ContactActivity.class);

//      startActivity(intent);
        //1. 啓動界面,並且等待結果的返回。
        startActivityForResult(intent, 2);
    }



    //用於獲取下一個界面返回的數據,這個方法由系統調用。
    /*
     * 參數一: 請求碼
     * 參數二: 結果碼
     * 參數三: 下一個界面傳遞過來的意圖對象
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode == 1){
            String content = data.getStringExtra("content");
            System.out.println("下一個界面返回數據了"+content);
            et_content.setText(content);
        }else if(requestCode == 2 ){
            et_contact.settext(content);
        }       
    }
}

第二個是短信模板界面所對應的activity代碼:

package com.itheima.smshelper;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class SmsActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sms);

        final String[]objects = {
                "這是短信內容----1",
                "這是短信內容----2",
                "這是短信內容----3",
                "這是短信內容----4",
                "這是短信內容----5",
                "這是短信內容----6",
                "這是短信內容----7",
                "這是短信內容----8",
                "這是短信內容----9",
                "這是短信內容----10",
                "這是短信內容----11"
        };

        ListView lv =(ListView)findViewById(R.id.lv);

        //參數一:上下文 ,參數二: 使用的條目佈局,這裏使用的是系統內置的佈局 ,參數三: 數據源,也就是顯示到條目上的數據
        lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, objects));


        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                Toast.makeText(SmsActivity.this, "position==="+position, 0).show();

                //獲取到點擊的條目內容
                String content = objects[position];



                //2. 設置返回的數據

                Intent data = new Intent();
                data.putExtra("content", content);
                if(短信是春節短信){
                    setResult(10, data);

                }else if(短信是中秋接短信){

                    setResult(11, data);
                }

                //3. 關閉當前界面
                finish();

                /*Intent intent = new Intent(SmsActivity.this, MainActivity.class);

                intent.putExtra("content", content);

                startActivity(intent);*/

            }
        });
    }
}

第三個是號碼選擇對應的activity代碼:

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ContactActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sms);

        final String[]objects = {
                "13810081001",
                "13810081002",
                "13810081003",
                "13810081004",
                "13810081005",
                "13810081006",
                "13810081007",
                "13810081008",
                "13810081009",
                "13810081010"
        };

        ListView lv =(ListView)findViewById(R.id.lv);

        //參數一:上下文 ,參數二: 使用的條目佈局,這裏使用的是系統內置的佈局 ,參數三: 數據源,也就是顯示到條目上的數據
        lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, objects));
    }
}

清單文件註冊頁面的代碼

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.itheima.smshelper"
    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.itheima.smshelper.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="com.itheima.smshelper.SmsActivity"></activity>
        <activity android:name="com.itheima.smshelper.ContactActivity"></activity>
    </application>

</manifest>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章