android - 在 Android 上使用 Kotlin 共享 Intent 文本

标签 android kotlin android-intent share-intent

我想使用 kotlin 共享 Intent 在我的 CardView 中共享文本,但是 kotlin 代码的最后一行有问题 代码

 val shareIntent = Intent()
            shareIntent.action = Intent.ACTION_SEND
            shareIntent.putExtra(Intent.EXTRA_STREAM, "ali")
            shareIntent.type = "text/plain"
            startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)))

这是代码中的问题

startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)))

请帮帮我

看图了解我

图片

https://ibb.co/jQwYXw

https://ibb.co/id0tXw

https://ibb.co/fbCU5G

适配器完整代码

class MyAdapter(context: Context, listItem: ArrayList<com.EliteTeam.comedytaste.Model.Movie>) : RecyclerView.Adapter<MyAdapter.MyViewHolder>() {

var Context = context
var movieList = listItem;
var layoutInflator = LayoutInflater.from(context)

override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): MyViewHolder {

    var inflateView = layoutInflator.inflate(R.layout.single_item, parent, false)

    return MyViewHolder(inflateView)
}

override fun onBindViewHolder(holder: MyViewHolder?, position: Int) {

    holder?.moviewTitle?.text = movieList[position].name
    holder?.movieDescription!!.text=   movieList[position].description
    //holder!!.cardImageView!!.background=   movieList[position].image

    holder?.onclick(Context, position)
    holder!!.cardImageView.setBackgroundResource(movieList[position].image)
}

override fun getItemCount(): Int {

    return movieList.size
}

class MyViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView) {

    var moviewTitle: TextView = itemView?.findViewById(R.id.movieTitleTextView)!!
    var movieDescription: TextView = itemView!!.findViewById(R.id.movieDescriptionTextView)
    var cardImageView: CardView = itemView!!.findViewById(R.id.imageCard)
    var share: ImageButton = itemView!!.findViewById(R.id.share)

    fun onclick(context: Context, position: Int) {
        cardImageView.setOnClickListener {

    }

       share.setOnClickListener {

           val shareIntent = Intent()
         shareIntent.action = Intent.ACTION_SEND
           shareIntent.putExtra(Intent.EXTRA_TEXT, "ali")
          shareIntent.type = "text/plain"


           startActivity(Intent.createChooser(shareIntent,"send to"))

}} }}

最佳答案

试试这个:- 仅当您必须发送图像等二进制数据时才使用 Intent.EXTRA_STREAM,如果您想发送文本,请使用 Intent.EXTRA_TEXT

    val shareIntent = Intent()
    shareIntent.action = Intent.ACTION_SEND
    shareIntent.type="text/plain"
    shareIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
    startActivity(Intent.createChooser(shareIntent,getString(R.string.send_to)))

如果在适配器中使用此代码,那么最后一行应该是 context.startActivity(Intent.createChooser(shareIntent,getString(R.string.send_to)))

关于android - 在 Android 上使用 Kotlin 共享 Intent 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47611945/

相关文章:

android - 从我的回收适配器的点击功能传递数据

java - Spring Boot @ConditionalOnProperty 不加载 bean

android-studio - 编译错误: "-Xcoroutines has no effect: coroutines are enabled anyway in 1.3 and beyond"

java - "getIntent()"- 它是如何工作的

javascript - <canvas> 绘图在 Firefox Mobile、HUAWEI Mediapad 10、Android 4.1.2 上损坏

java - 为什么 imageView 没有居中?

javascript - OnKeyboard 在 Sencha Touch 中隐藏

android - 我什么时候应该在 ContentProvider 使用的 SQLiteOpenHelper 上调用 close()

android-studio - 我可以在 Gradle 插件中配置 Kotlin 多平台模块吗?

android - 在同一应用中打开 branch.io 链接