短信大全簡單demo

在這裏插入圖片描述

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

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    String[] objects = {"最幸福的愛,就是找到了你。我縱容你的習慣,寬容你的折騰,愛着你的一切。你的心是我最好的房子,一起柴米油鹽,一起細水長流,然後一起相守着慢慢變老,從此陪伴一生。愛你,一輩子,永不改變。",
            "留一個特別的座位給你,在我懷裏;留一道特別的風景給你,在我眼裏;留一絲特別的思念給你,在我腦海裏;在有風的日子,我叮囑風兒,讓風替我親吻你;在有陽光的日子,讓陽光替我問候你;在有雨的日子,讓雨替我祝福你;我只想告訴你,我的生命裏不能沒有你!",
            "如果你一生氣,對方就立刻聽你的,那是怕你,怕是因爲愛。你一發嗲,對方就立刻聽你的,那是寵你,寵也是因爲愛。你一難過,對方就立刻聽你的,那是疼你,疼也是因爲愛。對你好有三個標誌,要麼怕你,要麼寵你,要麼疼你。",
            "我想讓清風把我的思念捎給你,可清風說我的思念太重,怕馱不動;我想讓星星把我的吻捎給你,可星星說我的吻太濃,怕會消融;我想讓流水把我的愛送給你,可流水說我的愛太熱烈,怕會蒸發;我只好借短信告訴你,我的情永遠爲你而鍾!",
            "路,有點霧,霧散留下了露珠,這露珠豐滿了愛的泥土。愛情路。彎彎路,彎得像一串珍珠,每一步都有簡單的領悟。我只想,每個朝朝暮暮,都和你共度。讓手心一直都熱乎乎,有種緩慢的幸福,伴隨一點辛苦。與你相守就是幸福!",
            "真正的愛情,要懂得珍惜:沒有誰和誰是天生就註定在一起的。一輩子其實不長,能遇到心愛的人,是多麼幸運的事,爲何不緊握着他的手呢。一輩子只愛一個人,不丟人。一顆心需要去溫暖另一顆心,坦誠相待,這樣纔可以幸福。"};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView listView = findViewById(R.id.lv);
        
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.item,R.id.textView ,objects);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String content = objects[position];
                Uri smsToUri = Uri.parse("smsto:");
                Intent intent = new Intent(Intent.ACTION_SENDTO, smsToUri);
                intent.putExtra("sms_body", content);
                startActivity(intent);
            }
        });
    }
}

以下界面使用ConstraintLayout可視化編程(就是用Android Studio畫出來的)
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">

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


</androidx.constraintlayout.widget.ConstraintLayout>

item.xml

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

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.13"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.message">
    <uses-permission android:name="android.permission.SEND_SMS"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

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