使用Kotlin将编辑文本的内容发送到电子邮件或文本的按钮

问题描述

我是Kotlin和Android Studio的完全新手,但我决定要编写一个可以在我的工作中使用的应用程序。我可以在Excel和VB中编写所需的内容,但是不幸的是,Android移动设备不支持宏,因此为什么要编写一个应用程序。 我已经弄清楚了如何创建页面(活动)以及如何在它们之间来回导航(大致),但是,我的最终目标是捕获用户将输入到文本字段中的数据,然后单击他们提交的数据。按钮,然后它将通过短信将他们输入的数据发送到预定义的电话号码(S),或者将文本文件发送到电子邮件地址。任何帮助将不胜感激。 请让我知道您需要我发布什么内容才能启用任何帮助 提前致谢 Code screenshot

package com.test.warehousecontrol
import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.*
import androidx.core.app.ActivityCompat
import android.telephony.SmsManager
import kotlinx.android.synthetic.main.activity_second.*

    class SecondActivity : AppCompatActivity()


    {
    override fun onCreate(savedInstanceState: Bundle?)


    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)
       //  if(ActivityCompat.checkSelfPermission(this,Manifest.permission.SEND_SMS)!=PackageManager.PERMISSION_GRANTED)
        val backbut = findViewById<Button>(R.id.backbut)
        backbut.setonClickListener {
            val intent = Intent(this,MainActivity::class.java)
            startActivity(intent)
        }


        }
        private fun getSendSmsIntent(phoneNumber: String,content: String?): Intent? {
            val uri = Uri.parse("smsto:$phoneNumber")
            val intent: Intent = Intent(Intent.ACTION_SENDTO,uri)
            intent.putExtra("sms_body",content)
            return getIntent(Intent,true)
            //  return getIntent(intent,true)

            val sub1: Button = findViewById<Button>(R.id.sub1)
            sub1.setonClickListener {

                val cust: String = cust.text.toString()
                val reg: String = reg.text.toString()
                val pal: String = pal.text.toString()
                val data: String =
                    "CUST : ".plus(cust).plus("\n").plus("REG : ").plus(reg).plus("\n").plus("PAL : ")
                        .plus(pal)
                startActivity(getSendSmsIntent("1234567",data))

            }
        }

        }

解决方法

发送短信:

val submit: Button = findViewById<Button>(R.id.sub1)
    submit.setOnClickListener {
        val cust: String = custTextField.text.toString()
        val reg: String = regTextField.text.toString()
        val pal: String = palTextField.text.toString()
        val data:String = "CUST : ".plus(cust).plus("\n").plus("REG : ").plus(reg).plus("\n").plus("PAL : ").plus(pal)
        startActivity(getSendSmsIntent("YOUR PHONE NUMBER HERE",data))
    }

要发送电子邮件:

val submit: Button = findViewById<Button>(R.id.sub1)
        submit.setOnClickListener {
            val cust: String = custTextField.text.toString()
            val reg: String = regTextField.text.toString()
            val pal: String = palTextField.text.toString()
            val data:String = "CUST : ".plus(cust).plus("\n").plus("REG : ").plus(reg).plus("\n").plus("PAL : ").plus(pal)
            startActivity(Intent.createChooser(getEmailIntent("USER@GMAIL.COM","SUBJECT",data),"Send mail"))
        }

private fun getSendSmsIntent(phoneNumber: String,content: String?): Intent? {
        val uri = Uri.parse("smsto:$phoneNumber")
        val intent = Intent(Intent.ACTION_SENDTO,uri)
        intent.putExtra("sms_body",content)
        return getIntent(intent,true)
    }

    private fun getEmailIntent(email: String,subject: String?,content: String?): Intent? {
        val intent = Intent(Intent.ACTION_SEND)
        intent.type = "message/rfc822"
        intent.putExtra(Intent.EXTRA_EMAIL,arrayOf(email))
        intent.putExtra(Intent.EXTRA_SUBJECT,subject)
        intent.putExtra(Intent.EXTRA_TEXT,true)
    }


    private fun getIntent(intent: Intent,isNewTask: Boolean): Intent? {
        return if (isNewTask) intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) else intent
    }

您的最终活动将是这样:

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity


class SecondActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)
        //  if(ActivityCompat.checkSelfPermission(this,Manifest.permission.SEND_SMS)!=PackageManager.PERMISSION_GRANTED)
        val backbut = findViewById<Button>(R.id.backbut)
        backbut.setOnClickListener {
            val intent = Intent(this,MainActivity::class.java)
            startActivity(intent)
        }
        val sub1: Button = findViewById<Button>(R.id.sub1)
        sub1.setOnClickListener {
            val cust: String = cust.text.toString()
            val reg: String = reg.text.toString()
            val pal: String = pal.text.toString()
            val data: String =
                "CUST : ".plus(cust).plus("\n").plus("REG : ").plus(reg).plus("\n").plus("PAL : ")
                    .plus(pal)
            startActivity(getSendSmsIntent("1234567",data))
        }
    }

    fun getSendSmsIntent(phoneNumber: String,true)
    }
    private fun getIntent(intent: Intent,isNewTask: Boolean): Intent? {
        return if (isNewTask) intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) else intent
    }
}