java - 自定义共享意向对话框这可能吗?

标签 java android android-intent

打开分享 Intent 代码

String xtype = "image/*";
Intent share = new Intent(Intent.ACTION_SEND);
share.setType(xtype);
Uri uri = FileProvider.getUriForFile(mContext, BuildConfig.APPLICATION_ID + ".provider", new File(tmpImageUri.getPath()));
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share via"));

它显示了这样的共享 Intent 对话框

Default Share Intent

但现在我想要这样

I want Like Sharing Intent

是否可以自定义共享意向对话框?

最佳答案

是的,可以创建自定义共享对话框。您只需要获取 IntentActivity 列表并根据您的要求进行自定义。对于示例,您可以执行以下操作。

Step 1: Prepare Intent

String urlToShare = "https://play.google.com/store/apps/details?id=com.yourapp.packagename";
final Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
// intent.putExtra(Intent.EXTRA_SUBJECT, "If any extra"); // NB: has no effect!
intent.putExtra(Intent.EXTRA_TEXT, "Let me recommend you this application \n\n" + urlToShare);

Step 2: Get activity list and set in ListAdapter.

final List<ResolveInfo> activities = getPackageManager().queryIntentActivities(intent, 0);

List<DialogItem> appNames = new ArrayList<DialogItem>();

    for (ResolveInfo info : activities) {
            appNames.add(new DialogItem(info.loadLabel(getPackageManager()).toString(),
                    info.loadIcon(getPackageManager())));
    }
final List<DialogItem> newItem = appNames;
ListAdapter adapter = new ArrayAdapter<DialogItem>(activity,
                android.R.layout.select_dialog_item, android.R.id.text1, newItem) {
            public View getView(int position, View convertView, ViewGroup parent) {
                //Use super class to create the View
                View v = super.getView(position, convertView, parent);
                TextView tv = v.findViewById(android.R.id.text1);
                tv.setText(newItem.get(position).app);
                tv.setTextSize(15.0f);
                //Put the image on the TextView
                tv.setCompoundDrawablesWithIntrinsicBounds(newItem.get(position).icon, null, null, null);

                //Add margin between image and text (support various screen densities)
                int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f);
                tv.setCompoundDrawablePadding(dp5);

                return v;
            }
};

注意:-确保 DialogItem 是您的模型类并且您必须在您的应用中创建。

public class DialogItem {
    public String app = "";
    public Drawable icon;

    public DialogItem(String name, Drawable drawable) {
        app = name;
        icon = drawable;
    }
}

Step 3: Set that adapter in your AlertDialog

AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle("Custom Sharing Dialog");
        builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                ResolveInfo info = activities.get(item);

                if (info.activityInfo.packageName.equals("com.facebook.katana")) {
                    Toast.makeText(activity, "Facebook Selected ", Toast.LENGTH_LONG).show();
                } else {

                    // start the selected activity
                    Log.i(TAG, "Hi..hello. Intent is selected");
                    intent.setPackage(info.activityInfo.packageName);
                    startActivity(intent);
                }
            }
        });

AlertDialog alert = builder.create();
alert.show();

输出:-

enter image description here

它的自定义共享对话框使用 AlertDialog但您可以根据您的要求使用自定义布局并创建 Dialog 来进行所有设置(UI、选择、主题等)类

关于java - 自定义共享意向对话框这可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52479752/

相关文章:

Java调用重载方法

android - 如何恢复申请状态

android - 从 GIT 中永久删除 .iml 文件

java - 如何手动填写国外 Collection

android - 某些设备上的 android 中的 "no such table"问题

java - 如何在 cassandra 2.2 中获得前 5 条记录

java - 在 Java 中使用数组的更好的最小值和最大值算法

java - 创建注释来执行常见操作并返回结果

android - putExtra 在 Intent 中使用 for 循环

java - Android 共享文件,通过电子邮件或其他应用程序发送