使用Kotlin在Android上共享意图文本

9
我想使用Kotlin通过共享意图在我的中共享文本,但是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"))

}} }}

2
问题实际上是什么? - Ankit Patidar
这段代码之前在Java中可以工作,但是当我将其翻译成Kotlin时,它就无法工作了。 - Ali Alsaadi
没有起作用...请提供更多细节。 - donfuxx
5个回答

26

试试这个:- 仅在发送二进制数据(如图像)时使用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)))


这有帮助吗? - Suraj Nair
你在哪里运行这段代码?你必须将这段代码放在Activity或Fragment中,它需要上下文才能运行。 - Suraj Nair
复制文本?你想做什么,从视图中复制文本吗? - Suraj Nair
是的,你知道我想在卡片视图中放置两个按钮,其中一个分享文本,另一个复制文本。我已经完成了分享按钮,现在我想要复制按钮。 - Ali Alsaadi
在 XML 视图中尝试这个:android:textIsSelectable="true" - Suraj Nair
显示剩余8条评论

3

在活动中分享文本

    val intent = Intent(Intent.ACTION_SEND)
    intent.putExtra(Intent.EXTRA_TEXT, "shareTextHere")
    intent.type = "text/plain"
    startActivity(Intent.createChooser(intent, "Share Via"))

在适配器中分享文本

    val intent = Intent(Intent.ACTION_SEND)
    intent.putExtra(Intent.EXTRA_TEXT, "shareTextHere")
    intent.type = "text/plain"
    context.startActivity(Intent.createChooser(intent, "Share Via"))

2

您可以直接使用这些函数:

fun showSharingDialogAsKotlin(text: String) {
    val intent = Intent()
    intent.action = Intent.ACTION_SEND
    intent.type = "text/plain"
    intent.putExtra(Intent.EXTRA_TEXT, text)
    startActivity(Intent.createChooser(intent, "Share with:"))
}

此外,如果您想展示您的图片并且有一个链接:
fun showSharingDialogAsKotlinWithURL(text: String, url: String) {
    val intent = Intent()
    intent.action = Intent.ACTION_SEND
    intent.type = "text/plain"
    intent.putExtra(Intent.EXTRA_TEXT, "$text: $url")
    startActivity(Intent.createChooser(intent, "Share with:"))
}

1
只需使用


context.startActivity

代替

startActivity

1
是的,这就是解决方案,与评论中的Suraj Nair相同,谢谢。 - Ali Alsaadi

0

你肯定可以转到“启动 Activity”的定义并查看重载,因为你没有传递可接受的参数之一。

我喜欢这样做,为了让导航代码到处都存在,就创建一个像这样的“IntentFactory”:

public class IntentFactory {

/**
 *
 * @param context
 * @return intent
 */
public static Intent getLoginIntent(Context context, boolean launchedFromNotification, boolean isApproveNotify, String idOfDetailToOpen, NewsModel notificationModel){
    Intent intent = new Intent(context, LoginActivity.class);
    intent.putExtra(Globals.INTENT_KEYS.KEY_FROM_BADGE_ACCESS, launchedFromNotification);
    intent.putExtra(Globals.INTENT_KEYS.KEY_ID_OF_DETAIL_TO_OPEN, idOfDetailToOpen);
    intent.putExtra(Globals.INTENT_KEYS.KEY_IS_APPROVE_NOTIFY, isApproveNotify);
    intent.putExtra(Globals.INTENT_KEYS.KEY_ALERT_NOTIFICATION_FOR_APPROVAL, notificationModel);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
    return intent;

}
/**
 *
 * @param filePath
 * @param subject
 * @param body
 * @return
 */
public static Intent getSendImageIntent(String filePath, String subject, String body){
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("image/jpg");
    intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + filePath));
    intent.putExtra(Intent.EXTRA_SUBJECT, subject);
    intent.putExtra(Intent.EXTRA_TEXT, body);
    return Intent.createChooser(intent, "Share File");

}
/**
 *
 * @param toEmailAddresses
 * @param subject
 * @param body
 * @param uris
 * @return
 */
public static Intent getEmailIntent(String[] toEmailAddresses, String subject, String body, ArrayList<Uri> uris) {
    Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_EMAIL, toEmailAddresses);
    intent.putExtra(Intent.EXTRA_SUBJECT, subject);
    intent.putExtra(Intent.EXTRA_TEXT, body);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

    if(uris != null) {
        intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);

    }

    return Intent.createChooser(intent, "Send mail...");

}
/**
 * Used to launch to app details screen for toggling of permissions or other things
 * @param context
 * @return
 */
public static Intent getShowAppDetailSettingsIntent(Context context){
    Intent intent = new Intent();
    intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
    intent.addCategory(Intent.CATEGORY_DEFAULT);
    intent.setData(Uri.parse("package:" + context.getPackageName()));
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
    return intent;

}

}

然后,您可以轻松使用StartActivity调用进行管理

      String email = SharedPreferencesManager.getInstance(getContext()).getSecuredPreference(REMEMBER_ME_USER_KEY);
    String subject = getString(R.string.message) + " " + model.getFirstName() + " " + model.getLastName();
    String body = getString(R.string.subject) + model.getFirstName() + " " + model.getLastName() + "\n" + model.getShootKeyUrl();

    startActivity(IntentFactory.getEmailIntent(new String[]{email}, subject, body, null));

所以,如果您对齐了正确的参数,那么就没问题了,但我仍然建议将其提取出来,以便更轻松地更新到像这样的工厂。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接