我可以对 xml - Android 中的所有按钮仅使用 1 个调用意图 (onclick) 吗?

Can I use only 1 call intent (onclick) for all the buttons in xml - Android?

我能否对 xml 中的所有按钮仅使用 1 个调用意图 (onclick),然后将传递给意图的值(phone 数字)基于哪个按钮被点击了?

而不是这个 Java 示例:

 public void CallRedCross(View call) {
    Intent callIntent = new Intent(Intent.ACTION_DIAL);
    callIntent.setData(Uri.parse("tel:09079338303"));
    startActivity(callIntent);
}

public void CallViracMPS(View call) {
        Intent callIntent = new Intent(Intent.ACTION_DIAL);
        callIntent.setData(Uri.parse("tel:09183242541"));
        startActivity(callIntent);
    }

所有按钮我应该只有 1 个 Call Intent。

public void CallPhone(View call) {
        Intent callIntent = new Intent(Intent.ACTION_DIAL);
        callIntent.setData(Uri.parse("tel: **BUTTON VALUE** "));
        startActivity(callIntent);
    }

当然,您可以轻松地将 Intent 设置为 class 级别变量,如果这会让您感觉更好的话,但为什么要对所有 Intent 使用一个 Intent。意图只不过是 "intention" 或将要采取的行动。我更喜欢制作一个 IntentFactory 来传递所需的参数和 returns 调用者使用的意图。这允许所有意图管理和活动更改都集中在一个中央位置。这是一个例子。

 /**
 *
 * Created by App Studio 35 on 6/27/17.
 */
public class IntentFactory {

    /**
     *
     * @param context
     * @return intent
     */
    public static Intent getLoginIntent(Context context, boolean launchedFromNotification, String idOfDetailToOpen){
        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.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
        return intent;

    }
    /**
     *
     * @param context
     * @return
     */
    public static Intent getSettingsIntent(Context context){
        Intent intent = new Intent(context, SettingsActivity.class);
        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;

    }

}

当然,您可以根据自己的个人意图对其进行自定义。但它不是一个沉重的对象,所以我更喜欢易读性和干净的可维护代码,而不是找到一种重用 Intent 的方法。虽然你当然可以将它移动到 class 级别,但如果你愿意的话,你只是引用 class 级别变量 Intent 如果愿意的话。

抱歉,我刚刚重读了一遍,所以您正在尝试使用按下的按钮来表示要传递的字符串。如果您的列表是静态的,您可以通过将值放入标记内来使用 android:tag 元素执行此操作。

但是,如果您的列表是动态的,那么您的 bindView 应该包含一个点击。您的 Adapter 构造函数应该采用一个用于点击回调的接口,您可以传递与索引关联的点击模型,当然在那个时候使用 model.getTelephoneNumber 传递给您的方法。这有意义吗?

根据要求为您的静态列表使用 TAG 方法,您可以这样做:

public void CallRedCross(View call) { 
    Intent callIntent = new Intent(Intent.ACTION_DIAL); 
    callIntent.setData(Uri.parse("tel:"+call.getTag())); 
    startActivity(callIntent); 
} 

当然我只是指给你使用标签,上面的代码就是你的代码。