单击ActionBar菜单项时,带有Android ActionBar的弹出对话框

时间:2019-02-19 17:24:18

标签: android android-actionbar

我正在构建一个需要在其中单击信息ActionBar项时通过另一个Android操作栏进行信息活动的应用。

这是activity_main_menu.xml的屏幕截图: Screenshot

activity_main_menu.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity">

        <item
            android:id="@+id/menu_item_info"
            android:icon="@drawable/info_outline"
            android:title="@string/information"
            android:titleCondensed="@string/info"
            app:showAsAction="ifRoom" />

        <item
            android:id="@+id/menu_item_share"
            android:title="@string/share"
            app:showAsAction="ifRoom"
            app:actionProviderClass="android.support.v7.widget.ShareActionProvider" />

</menu>

单击“ menu_item_info”时,这是我要发生的情况: Desired Result

activity_main.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    android:orientation="horizontal"
    tools:context=".MainActivity">

    <!-- <clip xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:clipOrientation="vertical"
        android:drawable="@drawable/bitcoin"
        android:gravity="top" /> -->

    <TextView
        android:id="@+id/tvTotal"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:padding="10dp"
        android:text="Text"
        android:textColor="#FFFFFF"
        android:textSize="30sp"
        android:textStyle="bold"
        app:fontFamily="sans-serif"
        app:lineHeight="60dp" />

    <ImageView
        android:id="@+id/ivBitcoin"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:contentDescription="You own text!"
        android:padding="10dp"
        app:srcCompat="@drawable/bitcoin" />

</LinearLayout>

MainActivity.java代码:

package com.shikhar_mainalee.iownallbitcoin;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.ShareActionProvider;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONObject;

import java.util.Collection;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {

    private TextView tvTotal;
    private ImageView ivBitcoin;
    private ShareActionProvider mShareActionProvider;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvTotal = findViewById(R.id.tvTotal);
        ivBitcoin = findViewById(R.id.ivBitcoin);
        Main.ivBitcoin = ivBitcoin;
        tvTotal.setText("You Own\n21,000,000 / 21,000,000\nBTC!");

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate menu resource file.
        getMenuInflater().inflate(R.menu.activity_main_menu, menu);

        // Locate MenuItem with ShareActionProvider
        MenuItem item = menu.findItem(R.id.menu_item_share);

        // Fetch and store ShareActionProvider
        ShareActionProvider shareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
        shareActionProvider.setShareIntent(createShareIntent());
        new ShareActionProvider(this).setShareIntent(null);

        // Return true to display menu
        return super.onCreateOptionsMenu(menu);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {

            case R.id.menu_item_info:
                // What code should go here?
                break;

            default:
                break;
        }

        return super.onOptionsItemSelected(item);

    }

    // Call to update the share intent
    private void setShareIntent(Intent shareIntent) {

        if (mShareActionProvider != null) {

            mShareActionProvider.setShareIntent(shareIntent);

        }

    }

}

解决此问题的最佳方法是什么?任何帮助表示赞赏!谢谢!

2 个答案:

答案 0 :(得分:0)

如果您想参加另一项活动,这是您应该做的:

  1. 创建另一个活动

  2. 创建打算去参加该活动

Intent myIntent = new Intent(this, SecondActivity.class);

  1. 您可以在主要活动的任何地方调用该Intent。就您而言:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) { case R.id.menu_item_info: startActivity(myIntent); // switch to second activity break; default: break; } return super.onOptionsItemSelected(item); }

to change the second activity action bar menu you should create a new Menu and like your main activity Inflate it in onCreateOptionsMenu

getMenuInflater().inflate(R.menu.activity_second_menu, menu);

答案 1 :(得分:0)

尝试一下:

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


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

这是android studio模板的示例。