Java基础-短信大全 1.适配器生成ListView(lv.setAdapter(adapter) 2.ListView点击事件(lv.setOnItemClickListener) 3.显示调用(intent.setAction("android.intent.action.SEND")

1. 构造Adapter 用于生成列表

   1.1构建item.xml 用于生成每一个TextView列表 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

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



</RelativeLayout>

   1.2 构建activity_mian.xml构建ListView列表 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

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



</RelativeLayout>

  1.3 构建adapter适配器,使用lv.setAdapter设置适配器 

2. 使用 lv.setOnItemClickListener 设置点击事件 

3. 创建意图对象,用于短信的发送,使用的action是显式的android.intent.action.SEND 

使用的是MainActivity.java

package com.example.a03_text_book;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    String objects[] = {"我喜欢你,喜欢的只是一个现在;我爱你,爱的却是一整个未来。如果是决定离开了一个人,那么你行动是要快一点,快刀斩乱麻;如果决定爱上一个人,时间要拉长一点,看清楚他是否真的适合你。"
            , "不需要海枯石烂的山盟海誓,只需一生一世的默默相守;不需要多么奢华的烛光晚餐,只需两个人,一桌粗茶淡饭;不需要有座别墅,面朝大海,春暖花开,只需一套小小房子,落地窗,一米阳光。这就是爱,平淡却幸福着;这就是爱,简单并快乐着。",
            "心理学家发现:一个人说的话若90%以上是废话,他就快乐。若废话不足50%,快乐感则不足。在交流中,没有太强目的性的语言,更容易让人亲近。----所以,我们每天都在找“幸福”。幸福是什么呢?大概就是找到了一个愿意听你说废话的人。",
            "大龄女青年的爱情宣言:夏天来了,秋天还会远么,找个男朋友然后挖个坑埋了,等到秋天长出好多好多个男朋友,一个揉肩、一个捶腿、一个做饭、一个哼小曲、一个收拾房子、一个陪我逛街、剩下的全都出去挣钱。"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //[1] 找到lv
        ListView lv = (ListView) findViewById(R.id.lv);
        //[2] 设置数据 先有数据
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.item, objects);
        //[3] 设置数据适配器
        lv.setAdapter(adapter);
        //[4]给listView设置点击事件 小技巧
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //[5]把点击条目的数据取出来 掌握一条原则
                String content = objects[position];
                //[6]跳转到发送短信页面 这里使用的是显示的intent
                Intent intent = new Intent();
                //[6.1]设置action 用于发送
                intent.setAction("android.intent.action.SEND");
                //[6.2]添加category
                intent.addCategory("android.intent.category.DEFAULT");
                //[6.3]设置type
                intent.setType("text/plain");

                //[6.4]传递数据
                intent.putExtra("sms_body", content);
                //[7]跳转到发送短信的页面
                startActivity(intent);
            }
        });

    }
}
原文地址:https://www.cnblogs.com/my-love-is-python/p/14427663.html