【发布时间】:2017-08-10 16:51:14
【问题描述】:
如何从 Activity 中的 Fragment 单击按钮 (btnTransmit) 触发 onOptionsItemSelected (menuItemTransmitMeterReading)。我需要按钮来做与 menuItem 相同的事情。放置所有额外的Stings 并开始新的活动。我是 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>
【问题讨论】:
-
我的建议是在片段中创建一个执行此操作的方法和 cal 方法。如果你愿意,我可以在一个答案中更好地解释。
-
是的,如果你能更好地解释一下,请告诉我
-
如果你尝试@YueMuc 的答案,他会回答我认为的逻辑,但如果你愿意,我可以创建我的答案,但逻辑是一样的。
-
如果你不觉得很难用一个例子画得更详细,因为当我尝试时,我的应用程序崩溃了
-
你可以试试我的回答。
标签: android android-fragments android-fragmentactivity