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

时间:2018-06-21 11:44:53

标签: java android app-store menuitem

我可以通过Button给出链接。但是我想在菜单项中添加一个到“率”的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

这是我的菜单项图片... rate us ite menu

enter image description here

这是给我们评分的代码

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

2 个答案:

答案 0 :(得分:0)

这是处理菜单项单击的方法

  1. 添加菜单文件

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
     MenuInflater inflater = getMenuInflater();
     inflater.inflate(R.menu.menu_file, menu);
     return true;
    }
    
  2. 然后在此处单击处理

    @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);
     }
    }
    

答案 1 :(得分:0)

您需要覆盖菜单功能,类似于下面的代码。

@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);
}