Java Android弹出窗口-后退按钮
我正在制作一个应用程序,我试图添加自定义弹出窗口,当我点击后退按钮时可以关闭它。问题是我不知道如何添加这个后退按钮。我附上一个文件,它显示了我现在拥有的和我想要实现的 我正在制作一个应用程序,我试图添加自定义弹出窗口,当我点击后退按钮时可以关闭它。问题是我不知道如何添加这个后退按钮。我附上一个文件,它显示了我现在拥有的和我想要实现的Java Android弹出窗口-后退按钮,java,android,fragment,popupwindow,android-listfragment,Java,Android,Fragment,Popupwindow,Android Listfragment,我正在制作一个应用程序,我试图添加自定义弹出窗口,当我点击后退按钮时可以关闭它。问题是我不知道如何添加这个后退按钮。我附上一个文件,它显示了我现在拥有的和我想要实现的 我正在制作一个应用程序,我试图添加自定义弹出窗口,当我点击后退按钮时可以关闭它。问题是我不知道如何添加这个后退按钮。我附上一个文件,它显示了我现在拥有的和我想要实现的 LayoutInflater充气机=(LayoutInflater)getActivity().getSystemService(布局充气机服务); popupV
LayoutInflater充气机=(LayoutInflater)getActivity().getSystemService(布局充气机服务);
popupView=充气机。充气(R.layout.activity\u事务,空);
最终PopupWindow-PopupWindow=新的PopupWindow(popupView,565760,真);
popupWindow.showAtLocation(popupView,重心,0,0);
Toolbar-Toolbar=popupView.findviewbyd(R.id.tool\u-bar);
toolbar.setTitleTextColor(Color.WHITE);
工具栏。设置标题(“新交易”);
int currentDay=1;
int currentmount=Integer.parseInt(actualChosenMonth.getText().toString().substring(0,2))-1;
int currentYear=Integer.parseInt(actualChosenMonth.getText().toString().substring(3,7));
SimpleDataFormat SimpleDataFormat=新SimpleDataFormat(“EE”);
日期日期=新日期(当前年、当前月、当前日-1);
字符串currentDayOfWeek=simpledateformat.format(日期);
字符串currentDateAsString=convertDateToString(currentDay、currentMonth、currentYear、currentDayOfWeek);
chooseTransactionDate=popupView.findviewbyd(R.id.chooseTransactionDate);
选择SetTransactionDate.setText(currentDateAsString);
选择SetTransactionDate.setOnClickListener(此);
chooseTodayTomorrow=popupView.findviewbyd(R.id.choosetodaytomrow);
选择setodaytomorrow.setOnClickListener(this);
chooseTransactionType=popupView.findviewbyd(R.id.chooseTransactionType);
选择SetTransactionType.setOnCheckedChangeListener(此);
ChosentTransactionExpense=popupView.findViewById(R.id.ChosentTransactionExpense);
ChosentTransactionIncome=popupView.findViewById(R.id.ChosentTransactionIncome);
transactionCategoryImage=popupView.findViewById(R.id.transactionCategoryImage);
chooseTransactionCategory=popupView.findviewbyd(R.id.chooseTransactionCategory);
ArrayAdapter chooseCategoryAdapter=新的ArrayAdapter(this.getActivity(),R.layout.popup\u微调器\u布局,getResources().getStringArray(R.array.list\u of_expenses));
选择TransactionCategory.setAdapter(选择类别适配器);
选择SetTransactionCategory.setOnItemSelectedListener(此);
chooseTransactionAmount=popupView.findviewbyd(R.id.chooseTransactionAmount);
选择TransactionAmount.requestFocus();
InputMethodManager imm=(InputMethodManager)getActivity().getSystemService(Context.INPUT\u方法\u服务);
imm.toggleSoftInput(InputMethodManager.SHOW\u FORCED,InputMethodManager.HIDE\u IMPLICIT\u ONLY);
chooseTransactionName=popupView.findviewbyd(R.id.chooseTransactionName);
saveTransactionButton=popupView.findViewById(R.id.saveTransactionButton);
saveTransactionButton.setOnClickListener(此);
布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="wrap_content"
android:background="@color/bacgroundColorPopup"
tools:context=".TransactionActivity">
<Button
android:id="@+id/saveTransactionButton"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_below="@+id/chooseTransactionName"
android:layout_centerInParent="true"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/rounded_button"
android:text="@string/save_transaction"
android:textSize="15sp" />
<EditText
android:id="@+id/chooseTransactionAmount"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_below="@id/transactionCategoryImage"
android:layout_alignParentLeft="true"
android:textColor="@color/BrownGrey"
android:paddingLeft="10dp"
android:hint="Amount"
android:maxLength="5"
android:textColorHint="@color/BrownGreyBrighter"
android:inputType="numberDecimal"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/custom_edit_text"
android:drawableRight="@drawable/calculator"
android:textSize="20sp" />
<EditText
android:id="@+id/chooseTransactionName"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_below="@id/chooseTransactionAmount"
android:layout_alignParentLeft="true"
android:textColor="@color/BrownGrey"
android:layout_marginTop="6dp"
android:layout_marginLeft="10dp"
android:paddingLeft="10dp"
android:hint="Notes"
android:textColorHint="@color/BrownGreyBrighter"
android:layout_marginRight="10dp"
android:background="@drawable/custom_edit_text"
android:drawableRight="@drawable/calculator"
android:textSize="20sp" />
<ImageView
android:id="@+id/transactionCategoryImage"
android:layout_margin="6dp"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_below="@id/chooseTransactionType"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp" />
<LinearLayout
android:id="@+id/dateButtons"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
<Button
android:id="@+id/chooseTransactionDate"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="62dp"
android:layout_marginRight="2dp"
android:layout_weight="1.6"
android:background="@drawable/rounded_button"
android:elevation="4dp"
android:padding="7dp"
android:text=""
android:textAllCaps="false"
android:textSize="16sp"
android:textStyle="bold" />
<Button
android:id="@+id/chooseTodayTommorow"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginLeft="2dp"
android:layout_marginTop="62dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:background="@drawable/rounded_button"
android:elevation="4dp"
android:padding="10dp"
android:text="@string/yesterday"
android:textAllCaps="false"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
<RadioGroup
android:id="@+id/chooseTransactionType"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/dateButtons"
android:orientation="horizontal">
<RadioButton
android:id="@+id/chosenTransactionExpense"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:background="@drawable/radio_selector"
android:button="@android:color/transparent"
android:checked="true"
android:elevation="4dp"
android:gravity="center"
android:padding="7dp"
android:text="@string/expense"
android:textColor="@drawable/text_color"
android:textSize="16sp" />
<RadioButton
android:id="@+id/chosenTransactionIncome"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_marginLeft="1dp"
android:layout_marginTop="5dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:background="@drawable/radio_selector"
android:button="@android:color/transparent"
android:elevation="4dp"
android:gravity="center"
android:padding="7dp"
android:text="@string/income"
android:textColor="@drawable/text_color"
android:textSize="16sp" />
</RadioGroup>
<Spinner
android:id="@+id/chooseTransactionCategory"
android:popupBackground="@color/bacgroundColorPopup"
android:layout_margin="6dp"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_below="@+id/chooseTransactionType"
android:layout_alignParentRight="true"
android:textAlignment="textEnd" />
<include
android:id="@+id/tool_bar"
layout="@layout/tool_bar" />
</RelativeLayout>
我尝试过这样做,但问题是我正在ListFragment类中创建弹出窗口,但它不起作用。如何将它作为图像按钮或类似的东西手动放置在活动\u事务中?我可以这样做,但我认为这不是最优雅的方法,但也许你是对的。我会尽量用那种方式做的
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="wrap_content"
android:background="@color/bacgroundColorPopup"
tools:context=".TransactionActivity">
<Button
android:id="@+id/saveTransactionButton"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_below="@+id/chooseTransactionName"
android:layout_centerInParent="true"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/rounded_button"
android:text="@string/save_transaction"
android:textSize="15sp" />
<EditText
android:id="@+id/chooseTransactionAmount"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_below="@id/transactionCategoryImage"
android:layout_alignParentLeft="true"
android:textColor="@color/BrownGrey"
android:paddingLeft="10dp"
android:hint="Amount"
android:maxLength="5"
android:textColorHint="@color/BrownGreyBrighter"
android:inputType="numberDecimal"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/custom_edit_text"
android:drawableRight="@drawable/calculator"
android:textSize="20sp" />
<EditText
android:id="@+id/chooseTransactionName"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_below="@id/chooseTransactionAmount"
android:layout_alignParentLeft="true"
android:textColor="@color/BrownGrey"
android:layout_marginTop="6dp"
android:layout_marginLeft="10dp"
android:paddingLeft="10dp"
android:hint="Notes"
android:textColorHint="@color/BrownGreyBrighter"
android:layout_marginRight="10dp"
android:background="@drawable/custom_edit_text"
android:drawableRight="@drawable/calculator"
android:textSize="20sp" />
<ImageView
android:id="@+id/transactionCategoryImage"
android:layout_margin="6dp"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_below="@id/chooseTransactionType"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp" />
<LinearLayout
android:id="@+id/dateButtons"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
<Button
android:id="@+id/chooseTransactionDate"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="62dp"
android:layout_marginRight="2dp"
android:layout_weight="1.6"
android:background="@drawable/rounded_button"
android:elevation="4dp"
android:padding="7dp"
android:text=""
android:textAllCaps="false"
android:textSize="16sp"
android:textStyle="bold" />
<Button
android:id="@+id/chooseTodayTommorow"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginLeft="2dp"
android:layout_marginTop="62dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:background="@drawable/rounded_button"
android:elevation="4dp"
android:padding="10dp"
android:text="@string/yesterday"
android:textAllCaps="false"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
<RadioGroup
android:id="@+id/chooseTransactionType"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/dateButtons"
android:orientation="horizontal">
<RadioButton
android:id="@+id/chosenTransactionExpense"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:background="@drawable/radio_selector"
android:button="@android:color/transparent"
android:checked="true"
android:elevation="4dp"
android:gravity="center"
android:padding="7dp"
android:text="@string/expense"
android:textColor="@drawable/text_color"
android:textSize="16sp" />
<RadioButton
android:id="@+id/chosenTransactionIncome"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_marginLeft="1dp"
android:layout_marginTop="5dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:background="@drawable/radio_selector"
android:button="@android:color/transparent"
android:elevation="4dp"
android:gravity="center"
android:padding="7dp"
android:text="@string/income"
android:textColor="@drawable/text_color"
android:textSize="16sp" />
</RadioGroup>
<Spinner
android:id="@+id/chooseTransactionCategory"
android:popupBackground="@color/bacgroundColorPopup"
android:layout_margin="6dp"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_below="@+id/chooseTransactionType"
android:layout_alignParentRight="true"
android:textAlignment="textEnd" />
<include
android:id="@+id/tool_bar"
layout="@layout/tool_bar" />
</RelativeLayout>