日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Intent回传值

發布時間:2024/1/8 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Intent回传值 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Intent回傳值:啟動一個Activity,當啟動的Activity關閉時,傳回值回到原Activity。以下一個小程序演示回傳值。

package com.itheima.smshelper;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
?? ?private EditText et_content;
?? ?private EditText et_number;

?? ?@Override
?? ?protected void onCreate(Bundle savedInstanceState) {
?? ??? ?super.onCreate(savedInstanceState);
?? ??? ?setContentView(R.layout.activity_main);
?? ??? ?et_content = (EditText) findViewById(R.id.et_content);
?? ??? ?et_number = (EditText) findViewById(R.id.et_number);
?? ?}

?? ?public void selectSms(View view) {
?? ??? ?Intent intent = new Intent(this, ListSmsActivity.class);
?? ??? ?// 開啟一個新的界面,并且獲取界面的返回值
?? ??? ?// startActivity(intent);
?? ??? ?// int requestCode 請求碼
?? ??? ?startActivityForResult(intent, 0);
?? ?}

?? ?public void selectNumber(View view) {
?? ??? ?Intent intent = new Intent(this, ListNumberActivity.class);
?? ??? ?// 開啟一個新的界面,并且獲取界面的返回值
?? ??? ?// startActivity(intent);
?? ??? ?// int requestCode 請求碼
?? ??? ?startActivityForResult(intent, 1);
?? ?}

?? ?@Override
?? ?protected void onActivityResult(int requestCode, int resultCode, Intent data) {
?? ??? ?if (data != null) {
?? ??? ??? ?String smsinfo = data.getStringExtra("smsinfo");
?? ??? ??? ?if (resultCode == 0) {
?? ??? ??? ??? ?et_content.setText(smsinfo);
?? ??? ??? ?} else if (resultCode == 1) {
?? ??? ??? ??? ?et_number.setText(smsinfo);
?? ??? ??? ?}
?? ??? ?}
?? ??? ?super.onActivityResult(requestCode, resultCode, data);
?? ?}

?? ?
?? ?public void sendSms(View view){
?? ??? ?String content = et_content.getText().toString().trim();
?? ??? ?String number = et_number.getText().toString().trim();
?? ??? ?SmsManager.getDefault().sendTextMessage(number, null, content, null, null);
?? ??? ?Toast.makeText(this, "發送成功", 0).show();
?? ?}
}

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;

public class ListNumberActivity extends Activity {
?? ?private ListView lv;
?? ?private String[] objects = {
?? ??? ??? ?"1234","34324","5643543","32424" };

?? ?@Override
?? ?protected void onCreate(Bundle savedInstanceState) {
?? ??? ?super.onCreate(savedInstanceState);
?? ??? ?setContentView(R.layout.activity_sms);
?? ??? ?lv = (ListView) findViewById(R.id.lv);
?? ??? ?lv.setAdapter(new ArrayAdapter<String>(this, R.layout.sms_item,
?? ??? ??? ??? ?R.id.tv_info, objects));
?? ??? ?
?? ??? ?lv.setOnItemClickListener(new OnItemClickListener() {

?? ??? ??? ?@Override
?? ??? ??? ?public void onItemClick(AdapterView<?> parent, View view,
?? ??? ??? ??? ??? ?int position, long id) {
?? ??? ??? ??? ?String smsinfo = objects[position];
?? ??? ??? ??? ?Intent data = new Intent();
?? ??? ??? ??? ?data.putExtra("smsinfo", smsinfo);
?? ??? ??? ??? ?//設置數據。
?? ??? ??? ??? ?setResult(1, data);
?? ??? ??? ??? ?//關閉掉當前的activity,并且回傳數據 onActivityResult().
?? ??? ??? ??? ?finish();
?? ??? ??? ?}
?? ??? ?});
?? ?}
}

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;

public class ListSmsActivity extends Activity {
?? ?private ListView lv;
?? ?private String[] objects = {
?? ??? ??? ?"玫瑰香香,情人黏黏,情話甜甜,情歌綿綿;花燈燦燦,禮花點點,好運連連,好夢圓圓。情人節喜逢元宵節,喜鵲登枝蝴蝶成雙鴛鴦成對雙喜臨門祝雙節快樂,合家團團圓圓,甜甜蜜蜜,開開心心,幸幸福福",
?? ??? ??? ?"情人節快到了,我精心挑選玫瑰花、百合花和滿天星,扎成一束鮮花隨短信送給你,火紅的玫瑰代表熱烈奔放,潔白的百合代表百年好合,小小的滿天星代表幸福美好。愿你的情人節熱烈奔放,你們的愛情百年好合,你們的生活幸福美滿。預祝情人節快樂",
?? ??? ??? ?"^o^滿天星光,最愛你許過愿望的那一顆,鮮花綻放,最愛你摘下微笑的那一朵,曼妙旋律,最愛你感動落淚的那一段,親愛的,情人節快樂,愛你。 ",
?? ??? ??? ?"^o^寶貝,情人節到了,送你一束玫瑰,用真心塑料紙包扎,系上快樂彩帶,噴點愛的香水,插一張真情卡片,寫著:寶貝,愿我的愛能帶給你一生的快樂!" };

?? ?@Override
?? ?protected void onCreate(Bundle savedInstanceState) {
?? ??? ?super.onCreate(savedInstanceState);
?? ??? ?setContentView(R.layout.activity_sms);
?? ??? ?lv = (ListView) findViewById(R.id.lv);
?? ??? ?lv.setAdapter(new ArrayAdapter<String>(this, R.layout.sms_item,
?? ??? ??? ??? ?R.id.tv_info, objects));
?? ??? ?
?? ??? ?lv.setOnItemClickListener(new OnItemClickListener() {

?? ??? ??? ?@Override
?? ??? ??? ?public void onItemClick(AdapterView<?> parent, View view,
?? ??? ??? ??? ??? ?int position, long id) {
?? ??? ??? ??? ?String smsinfo = objects[position];
?? ??? ??? ??? ?Intent data = new Intent();
?? ??? ??? ??? ?data.putExtra("smsinfo", smsinfo);
?? ??? ??? ??? ?//設置數據。
?? ??? ??? ??? ?setResult(0, data);
?? ??? ??? ??? ?//關閉掉當前的activity,并且回傳數據 onActivityResult().
?? ??? ??? ??? ?finish();
?? ??? ??? ?}
?? ??? ?});
?? ?}
}

主界面

<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"
??? tools:context=".MainActivity" >

??? <EditText
??????? android:id="@+id/et_number"
??????? android:layout_width="fill_parent"
??????? android:layout_height="wrap_content"
??????? android:hint="請輸入電話號碼" />

??? <EditText
??????? android:id="@+id/et_content"
??????? android:layout_width="fill_parent"
??????? android:layout_height="wrap_content"
??????? android:hint="請輸入短信的內容"
??????? android:inputType="textMultiLine"
??????? android:lines="5" />

??? <Button
??????? android:layout_width="fill_parent"
??????? android:layout_height="wrap_content"
??????? android:onClick="selectSms"
??????? android:text="選擇短信" />

??? <Button
??????? android:layout_width="fill_parent"
??????? android:layout_height="wrap_content"
??????? android:onClick="selectNumber"
??????? android:text="選擇電話號碼" />

??? <Button
??????? android:onClick="sendSms"
??????? android:layout_width="fill_parent"
??????? android:layout_height="wrap_content"
??????? android:text="發送短信" />

</LinearLayout>

可選短信

<?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:id="@+id/lv"
??????? android:layout_width="match_parent"
??????? android:layout_height="match_parent" >
??? </ListView>

</LinearLayout>


ListView的Item布局

<?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="wrap_content"
??? android:orientation="vertical" >

??? <TextView
??????? android:id="@+id/tv_info"
??????? android:layout_width="match_parent"
??????? android:layout_height="wrap_content"
??????? android:drawableLeft="@android:drawable/star_big_on"
??????? android:gravity="center_vertical"
??????? android:text="我愛你。" />

</LinearLayout>

總結

以上是生活随笔為你收集整理的Intent回传值的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。