1. 程式人生 > >Android核心技術-day06-04-簡訊助手

Android核心技術-day06-04-簡訊助手

package com.gaozewen.smshelper;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView mTv_content;

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

        mTv_content = (TextView) findViewById(R.id.tv_content);
    }

    /**
     * 選擇簡訊
     *
     * @param view
     */
    public void selectSms(View view) {
        Intent intent = new Intent(this, SelectSMSActivity.class);
        startActivityForResult(intent, 0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (resultCode == 0) {
            if (data != null) {
                String text = data.getStringExtra("text");
                mTv_content.setText(text);
            } else {
                mTv_content.setText("沒有選擇簡訊");
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
package com.gaozewen.smshelper;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class SelectSMSActivity extends AppCompatActivity {

    private ListView mLv;

    private String[] smsContents = {"猴年大吉","猴年happy","天天發財","彩票中獎"};

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

        mLv = (ListView) findViewById(R.id.lv);
        mLv.setAdapter(new ArrayAdapter<String>(this,R.layout.item_textview,smsContents));
        mLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String smsContent = smsContents[i];
                Intent data = new Intent();
                data.putExtra("text",smsContent);
                setResult(0,data);
                finish();
            }
        });
    }
}