当用户从活动中点击回来时,我试图从自定义对话框中获取输入。
我重写onBackPressed()
方法,我看到对话框,但活动就在它之后关闭。
我还尝试在调用对话框后将super.onBackPressed();
移动到
@Override
public void onBackPressed() {
super.onBackPressed();
if(enableTimer) {
countDownTimer.cancel();
}
if(enableHistorySaving){
showCustomDialog(vp.getCurrentItem());
// i want the activity will be closed here after getting input.
}
}
showCustomDialogMethod:
public void showCustomDialog(final int position){
final Dialog dialog = new Dialog(ExerciseActivity.this);
dialog.setContentView(R.layout.enter_data);
dialog.setTitle("save your data...");
Button okButton = (Button) dialog.findViewById(R.id.ok);
okButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final int numOfSets = Integer.valueOf(((EditText)dialog.findViewById(R.id.numOfSetsDialog)).getText().toString());
final String wights = ((EditText)dialog.findViewById(R.id.wights)).getText().toString();
saveToExcelFile(dayExercises.get(position),wights,numOfSets);
dialog.dismiss();
}
});
Button cencelButton = (Button) dialog.findViewById(R.id.cencel);
cencelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
答案 0 :(得分:1)
在退出之前,您似乎正在显示一个用于保存用户数据的对话框。
在对话框按钮的super.onBackPressed();
方法中添加此行setOnClickListener
注意:它必须是侦听器方法中的最后一行
答案 1 :(得分:0)
您可以使用片段通过此代码显示警报
@Override
public void onBackPressed() {
doubleBackToExitPressed(R.id.content_dashboard);
}
public void doubleBackToExitPressed(int content) {
if (getFragmentManager().getBackStackEntryCount() > 1) {
FragmentManager manager = getFragmentManager();
manager.beginTransaction()
.remove(manager.findFragmentById(content))
.commit();
manager.popBackStack();
} else {
getFragmentManager().beginTransaction()
.add(content, Question.newInstance()
.addToBackStack(null).commit();
}
}
main_activity.xml:
<FrameLayout
android:id="@+id/content_dashboard"
android:layout_width="match_parent"
android:layout_height="match_parent" />
question.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 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:id="@+id/question_layout"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_marginLeft="@dimen/dimen_40dp"
android:layout_marginRight="@dimen/dimen_40dp"
android:layout_marginTop="@dimen/dimen_16dp"
android:background="@color/white"
android:clickable="true"
android:orientation="vertical"
android:tag="question"
app:cardBackgroundColor="@color/colorPrimaryDark">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="100">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="20"
android:background="@color/colorPrimaryDark"
android:weightSum="100">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="20"
android:background="@color/colorGrayLight"
android:orientation="vertical"
android:weightSum="100">
<TextView
android:id="@+id/txt_title_dialog"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="@dimen/dimen_16dp"
android:layout_weight="32"
android:clickable="true"
android:gravity="center_vertical"
android:text="@string/is_login_or"
android:textColor="@color/black"
android:textSize="16sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="40"
android:clickable="true"
android:orientation="horizontal"
android:paddingLeft="12dp"
android:weightSum="100">
<TextView
android:id="@+id/txt_cancel_question_answer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="40"
android:gravity="center"
android:text="@string/no"
android:textColor="@color/black"
android:textSize="12sp" />
<TextView
android:id="@+id/txt_ok_question_answer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="40"
android:gravity="center"
android:text="@string/yes"
android:textColor="@color/black"
android:textSize="12sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="20" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="28" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="40" />
</LinearLayout>
</android.support.v7.widget.CardView>
question.java:
public class Question extends Fragment {
@BindView(R.id.user_question)
LottieAnimationView user_question;
@BindView(R.id.txt_title_dialog)
TextView txt_title_dialog;
@BindView(R.id.txt_cancel_question_answer)
TextView txt_cancel_question_answer;
@BindView(R.id.txt_ok_question_answer)
TextView txt_ok_question_answer;
public static Question newInstance() {
Question question = new Question();
return question;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.question, parent, false);
ButterKnife.bind(this, view);
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
}
@OnClick(R.id.txt_cancel_question_answer)
public void setTxt_cancel_question_answer() {
removeQuestion();
// you can set action for cancel this is
}
@OnClick(R.id.txt_ok_question_answer)
public void setTxt_ok_question_answer() {
removeQuestion();
// you can set action for ok this is
}
public void removeQuestion() {
FragmentManager manager = getFragmentManager();
manager.beginTransaction()
.remove(manager.findFragmentById(content))
.commit();
manager.popBackStack();
}
}
我用黄油刀你可以看到网站并学习! http://jakewharton.github.io/butterknife/
答案 2 :(得分:-1)
您可以调用finish()方法。接受用户的输入后,将按下ok按钮完成活动。
@Override
public void onBackPressed()
{
if(enableTimer)
{
countDownTimer.cancel();
}
if(enableHistorySaving)
{
showCustomDialog(vp.getCurrentItem());
}
}
public void showCustomDialog(final int position){
final Dialog dialog = new Dialog(ExerciseActivity.this);
dialog.setContentView(R.layout.enter_data);
dialog.setTitle("save your data...");
Button okButton = (Button) dialog.findViewById(R.id.ok);
okButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final int numOfSets = Integer.valueOf(((EditText)dialog.findViewById(R.id.numOfSetsDialog)).getText().toString());
final String wights = ((EditText)dialog.findViewById(R.id.wights)).getText().toString();
saveToExcelFile(dayExercises.get(position),wights,numOfSets);
dialog.dismiss();
finish();
}
});
Button cencelButton = (Button) dialog.findViewById(R.id.cencel);
cencelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
这是更新的答案。请看看@keiloda