如何在活动按钮单击中触发onOptionsItemSelected(片段)

时间:2017-08-10 08:00:26

标签: android android-fragments android-fragmentactivity

如何从Activity中的Fragment触发onOptionsItemSelected(menuItemTransmitMeterReading)单击按钮(btnTransmit)。我需要按钮与menuItem一样。放置所有extraStings并开始新的活动。我是android的新手,片段对我来说非常难。  
片段

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.account_detail, container, false);
        if (mItem != null) {
            ((TextView) rootView.findViewById(R.id.account_detail)).setText(mItem.details);
        }
        return rootView;
    }
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.menu_account, menu);
        menuItemAccountDelete = menu.findItem(R.id.action_account_delete);
        menuItemTransmitMeterReading = menu.findItem(R.id.action_transmit_meter_readings);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.equals(menuItemAccountDelete)) {
            FragmentManager manager = getActivity().getSupportFragmentManager();
            MyDeleteAccountDialog myDialog = new MyDeleteAccountDialog();
            myDialog.LS = mItem.ls;
            myDialog.show(manager, "dialog");
        } else if (item.equals(menuItemTransmitMeterReading)) {
            Intent intent = new Intent(getActivity(), ActivityAccountTransmitMeterReading.class);
            intent.putExtra(ACCOUNT_LS, mItem.ls);
            intent.putExtra(ACCOUNT_NUMBER_OF_ZONES, mItem.MeterZonesCount);
            intent.putExtra(ACCOUNT_LAST_METER_READING, mItem.LastMeterReading);
            intent.putExtra(ACCOUNT_METER_NUMBER_OF_DIGITS, mItem.MeterNumberOfDigits);
            intent.putExtra(ACCOUNT_ADDRESS, mItem.Address);
            intent.putExtra(ACCOUNT_RMES, mItem.R_mes);
            intent.putExtra(ACCOUNT_FKVT1, mItem.FKVT1);
            intent.putExtra(ACCOUNT_FKVT2, mItem.FKVT2);
            intent.putExtra(ACCOUNT_FKVT3, mItem.FKVT3);
            intent.putExtra(ACCOUNT_FIO, mItem.fio);
            if(mItem.LastMeterReading.contains("TEST")){
                FragmentManager manager = getActivity().getSupportFragmentManager();
                MyTransmitMeterReadingDialog myTransmitMeterReadingDialog = new MyTransmitMeterReadingDialog();
                myTransmitMeterReadingDialog.show(manager,"dialog");
            }
            else {
                startActivityForResult(intent, REQUEST_ACCOUNT_TRANSMIT_METER_READING);

            }
        }

        return super.onOptionsItemSelected(item);
    }

layoutFragment.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/account_detail"
    style="?android:attr/textAppearanceLarge"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="0dp"
    android:textIsSelectable="true"
    tools:context="local.soe.itps02.soebilling.FragmentAccountDetail" /><br>

layoutActivity.xml

<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
    tools:context="local.soe.itps02.soebilling.ActivityAccountDetail"
    tools:ignore="MergeRootFrame">
    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">
        <android.support.v7.widget.Toolbar
            android:id="@+id/detail_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/AppTheme.PopupOverlay" />
    </android.support.design.widget.AppBarLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="70dp">
        <Button
            android:id="@+id/btnTransmit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="OnActionButtonClick"
            android:layout_weight="1"
            android:text="Transmit" />
        <Button
            android:id="@+id/btnPay"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="OnActionButtonClick"
            android:layout_weight="1"
            android:text="Pay"
            />
    </LinearLayout>
    <android.support.v4.widget.NestedScrollView
        android:id="@+id/account_detail_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="65dp"
        android:overScrollMode="never"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
    </android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>

1 个答案:

答案 0 :(得分:1)

就像我在评论中所说,你可以这样做,首先在mainActivity中创建这个方法:

public void startintent(){
Intent intent = new Intent(MainActivity.this, ActivityAccountTransmitMeterReading.class);
            intent.putExtra(ACCOUNT_LS, mItem.ls);
            intent.putExtra(ACCOUNT_NUMBER_OF_ZONES, mItem.MeterZonesCount);
            intent.putExtra(ACCOUNT_LAST_METER_READING, mItem.LastMeterReading);
            intent.putExtra(ACCOUNT_METER_NUMBER_OF_DIGITS, mItem.MeterNumberOfDigits);
            intent.putExtra(ACCOUNT_ADDRESS, mItem.Address);
            intent.putExtra(ACCOUNT_RMES, mItem.R_mes);
            intent.putExtra(ACCOUNT_FKVT1, mItem.FKVT1);
            intent.putExtra(ACCOUNT_FKVT2, mItem.FKVT2);
            intent.putExtra(ACCOUNT_FKVT3, mItem.FKVT3);
            intent.putExtra(ACCOUNT_FIO, mItem.fio);

}

内部按钮onclickListener或onOptionsItemSelected()只需调用:

startintent()

或者如果你想调用片段内的按钮onclickListener或onOptionsItemSelected()

MainActivity.startintent()