简体   繁体   English

如何将App Store链接添加到菜单项?

[英]How to add App Store link to menu item?

I can give link via Button.But I want to add a App store link to "rateus" in menu item.(please see attached image)here is the button code in MainActivity.java.This is not working for menu item.please help me. 我可以通过Button给出链接。但是我想向菜单项中的“ rateus”添加一个App store链接。(请参见所附图片)这是MainActivity.java中的按钮代码。这不适用于菜单项。请帮助我。

//rateus button
        android.widget.Button ratebutton = (android.widget.Button) findViewById(R.id.id_rateus);
        ratebutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    startActivity(new Intent(Intent.ACTION_VIEW,
                            android.net.Uri.parse("market://play.google.com/store/apps/details?id=com.slsindupotha&hl=en")));
                }catch (android.content.ActivityNotFoundException e){
                    startActivity(new Intent(Intent.ACTION_VIEW,
                            android.net.Uri.parse("https://play.google.com/store/apps/details?id=com.slsindupotha&hl=en")));
                }
            }
        });
//end rateus button code

here is my menu item image... rate us ite menu 这是我的菜单项图像... 评价我们的菜单

在此处输入图片说明

here is the code for rate us item 这是给我们评分的代码

<item
        android:id="@+id/id_rateus"
        android:orderInCategory="100"
        android:title="@string/action_rateus"
        android:textAllCaps="false"
        app:showAsAction="ifRoom" />

This is how you can handle a menu item click 这是处理菜单项单击的方法

  1. Inflate the menu file 膨胀菜单文件

     @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu_file, menu); return true; } 
  2. Then handle onCLick here 然后在这里处理onCLick

     @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle item selection switch (item.getItemId()) { case R.id.id_rateus: //handle onclick event for intent to playstore return true; default: return super.onOptionsItemSelected(item); } } 

You need to override the menu functions, something like the code below. 您需要覆盖菜单功能,类似于下面的代码。

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds appModels to the action bar if it is present.
    this.menu = menu;
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    switch (id) {
        case R.id.share:

            Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
            sharingIntent.setType("text/plain");
            String shareBodyText = "Check it out. Your message goes here";
            sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Subject here");
            sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBodyText);
            startActivity(Intent.createChooser(sharingIntent, "Shearing Option"));
            return true;

        case R.id.id_rateus:
            try {
                    startActivity(new Intent(Intent.ACTION_VIEW,                                
android.net.Uri.parse("market://play.google.com/store/apps/details?id=com.slsindupotha&hl=en")));
                }catch (android.content.ActivityNotFoundException e){
                    startActivity(new Intent(Intent.ACTION_VIEW,
                            android.net.Uri.parse("https://play.google.com/store/apps/details?id=com.slsindupotha&hl=en")));
                }

            return true;
    }

    return super.onOptionsItemSelected(item);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM