使用 Kotlin 将编辑文本的内容发送到电子邮件或文本的按钮
Button to send contents of edit text to email or text using Kotlin
我是 Kotlin 和 Android Studio 的新手,但我决定要编写一个应用程序供我的工作使用。我可以在 Excel 和 VB 中编写我需要的内容,但不幸的是 Android 移动设备不支持宏,因此我想编写一个应用程序。
我已经弄清楚了如何创建页面(活动)以及如何在它们之间来回导航(大致),但是,我的最终目标是捕获用户将输入到文本字段中的数据,然后一旦他们单击提交按钮,然后它将他们通过 SMS 输入的数据发送到预定义的 phone 号码 (S),或者作为文本文件发送到电子邮件地址。任何帮助将不胜感激。
请让我知道您需要我做什么 post 以启用任何帮助
提前致谢
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, content)
return getIntent(intent, true)
}
private fun getIntent(intent: Intent, isNewTask: Boolean): Intent? {
return if (isNewTask) intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) else intent
}
你的最终 Activity 会是这样的:
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, 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 getIntent(intent: Intent, isNewTask: Boolean): Intent? {
return if (isNewTask) intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) else intent
}
}
我是 Kotlin 和 Android Studio 的新手,但我决定要编写一个应用程序供我的工作使用。我可以在 Excel 和 VB 中编写我需要的内容,但不幸的是 Android 移动设备不支持宏,因此我想编写一个应用程序。 我已经弄清楚了如何创建页面(活动)以及如何在它们之间来回导航(大致),但是,我的最终目标是捕获用户将输入到文本字段中的数据,然后一旦他们单击提交按钮,然后它将他们通过 SMS 输入的数据发送到预定义的 phone 号码 (S),或者作为文本文件发送到电子邮件地址。任何帮助将不胜感激。 请让我知道您需要我做什么 post 以启用任何帮助 提前致谢 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, content)
return getIntent(intent, true)
}
private fun getIntent(intent: Intent, isNewTask: Boolean): Intent? {
return if (isNewTask) intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) else intent
}
你的最终 Activity 会是这样的:
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, 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 getIntent(intent: Intent, isNewTask: Boolean): Intent? {
return if (isNewTask) intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) else intent
}
}