DialogFragment 创建多个对话框 windows 而不是一个
DialogFragment creates several dialog windows instead of one
我有一个选项卡 activity,其中包含两个底部选项卡。 FrameLayout 充满了我创建的特殊布局文件。这两个布局文件是 运行 by java classes 扩展 Fragment class。这是我现在所拥有的的简要描述。
现在是关于问题的详细信息。布局文件包含两个 EditText 字段,单击它们时必须创建一个 DatePickerDialog。我已经使用 DialogFragment class 实现了对话框,一个用于两个文本字段。 Hovewer,当我单击编辑文本时,它们会产生多个 DatePicker 对话框,而不是一个,并且不清楚背后的逻辑是什么。它可能会产生 2 个、3 个甚至 4 个对话框 windows。
我该如何解决这个问题?这是我的代码:
Activity
public class Activity extends AppCompatActivity {
FragmentTabHost mFragmentTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quick_calculation);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mFragmentTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mFragmentTabHost.setup(QuickCalculationActivity.this, getSupportFragmentManager(), android.R.id.tabcontent);
mFragmentTabHost.addTab(mFragmentTabHost.newTabSpec("days").setIndicator("DAYS"), DaysTabView.class, null);
mFragmentTabHost.addTab(mFragmentTabHost.newTabSpec("date").setIndicator("DATE"), DateTabView.class, null);
}
}
其中一个 tabView classes
public class DaysTabView extends Fragment {
/*
* some code was not included to make easir to read
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.days_tab_view, container, false);
mFromDate = (EditText) v.findViewById(R.id.fromDate);
mFromDate.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mFromDate.setText("");
SetDateToDefault(1); //some method
Bundle args = new Bundle();
args.putInt("day", day1);
args.putInt("month", month1);
args.putInt("year", year1);
myDatePickerDialogFragment d1 = new myDatePickerDialogFragment();
d1.setArguments(args);
d1.setOnDateSetListener(datePickerListener1); //onDateSet Listener that is not included in this question
d1.show(getFragmentManager(), "days");
return false;
}
});
}
}
和DialogFragmentclass
public class myDatePickerDialogFragment extends DialogFragment{
int mDay, mMonth, mYear;
OnDateSetListener onSetDate;
public myDatePickerDialogFragment(){
}
@Override
public void setArguments(Bundle args) {
super.setArguments(args);
mDay = args.getInt("day");
mMonth = args.getInt("month");
mYear = args.getInt("year");
}
public void setOnDateSetListener(OnDateSetListener setDate){
onSetDate = setDate;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new DatePickerDialog(getActivity(), onSetDate, mYear, mMonth, mDay);
}
}
return setOnTouchListener 中的 true 而不是 false。
if(event.getAction() == MotionEvent.ACTION_UP){
// Do what you want
return true;
}
return false;
}
应该是这样的:
mFromDate.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP){
mFromDate.setText("");
SetDateToDefault(1); //some method
Bundle args = new Bundle();
args.putInt("day", day1);
args.putInt("month", month1);
args.putInt("year", year1);
myDatePickerDialogFragment d1 = new myDatePickerDialogFragment();
d1.setArguments(args);
d1.setOnDateSetListener(datePickerListener1); //onDateSet Listener that is not included in this question
d1.show(getFragmentManager(), "days");
return true;
}
return false;
}
});
我建议在 EditTexts 上将 onTouchListener
更改为 onClickListener
。背后的原因是触摸监听器会在每次事件发生时触发(例如手指按下、手指移动、手指向上等),而点击监听器只会触发一次。
所以在 DaysTabView
class 中,更改为:
mFromDate.setOnClickListener(new View.OnClickListener() {
@Override
public boolean onClick(View v) {
mFromDate.setText("");
SetDateToDefault(1); //some method
Bundle args = new Bundle();
args.putInt("day", day1);
args.putInt("month", month1);
args.putInt("year", year1);
myDatePickerDialogFragment d1 = new myDatePickerDialogFragment();
d1.setArguments(args);
d1.setOnDateSetListener(datePickerListener1); //onDateSet Listener that is not included in this question
d1.show(getFragmentManager(), "days");
return false;
}
});
更改您的 onTouchListener
代码
mFromDate.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
final int actionPeformed = ev.getAction();
switch(actionPeformed){
case MotionEvent.ACTION_UP:{
mFromDate.setText("");
SetDateToDefault(1); //some method
Bundle args = new Bundle();
args.putInt("day", day1);
args.putInt("month", month1);
args.putInt("year", year1);
myDatePickerDialogFragment d1 = new myDatePickerDialogFragment();
d1.setArguments(args);
d1.setOnDateSetListener(datePickerListener1); //onDateSet Listener that is not included in this question
d1.show(getFragmentManager(), "days");
break;
}
return true;
}
});
也许你应该使用 ClickListener 而不是 TouchListener
我有一个选项卡 activity,其中包含两个底部选项卡。 FrameLayout 充满了我创建的特殊布局文件。这两个布局文件是 运行 by java classes 扩展 Fragment class。这是我现在所拥有的的简要描述。
现在是关于问题的详细信息。布局文件包含两个 EditText 字段,单击它们时必须创建一个 DatePickerDialog。我已经使用 DialogFragment class 实现了对话框,一个用于两个文本字段。 Hovewer,当我单击编辑文本时,它们会产生多个 DatePicker 对话框,而不是一个,并且不清楚背后的逻辑是什么。它可能会产生 2 个、3 个甚至 4 个对话框 windows。
我该如何解决这个问题?这是我的代码:
Activity
public class Activity extends AppCompatActivity {
FragmentTabHost mFragmentTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quick_calculation);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mFragmentTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mFragmentTabHost.setup(QuickCalculationActivity.this, getSupportFragmentManager(), android.R.id.tabcontent);
mFragmentTabHost.addTab(mFragmentTabHost.newTabSpec("days").setIndicator("DAYS"), DaysTabView.class, null);
mFragmentTabHost.addTab(mFragmentTabHost.newTabSpec("date").setIndicator("DATE"), DateTabView.class, null);
}
}
其中一个 tabView classes
public class DaysTabView extends Fragment {
/*
* some code was not included to make easir to read
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.days_tab_view, container, false);
mFromDate = (EditText) v.findViewById(R.id.fromDate);
mFromDate.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mFromDate.setText("");
SetDateToDefault(1); //some method
Bundle args = new Bundle();
args.putInt("day", day1);
args.putInt("month", month1);
args.putInt("year", year1);
myDatePickerDialogFragment d1 = new myDatePickerDialogFragment();
d1.setArguments(args);
d1.setOnDateSetListener(datePickerListener1); //onDateSet Listener that is not included in this question
d1.show(getFragmentManager(), "days");
return false;
}
});
}
}
和DialogFragmentclass
public class myDatePickerDialogFragment extends DialogFragment{
int mDay, mMonth, mYear;
OnDateSetListener onSetDate;
public myDatePickerDialogFragment(){
}
@Override
public void setArguments(Bundle args) {
super.setArguments(args);
mDay = args.getInt("day");
mMonth = args.getInt("month");
mYear = args.getInt("year");
}
public void setOnDateSetListener(OnDateSetListener setDate){
onSetDate = setDate;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new DatePickerDialog(getActivity(), onSetDate, mYear, mMonth, mDay);
}
}
return setOnTouchListener 中的 true 而不是 false。
if(event.getAction() == MotionEvent.ACTION_UP){
// Do what you want
return true;
}
return false;
}
应该是这样的:
mFromDate.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP){
mFromDate.setText("");
SetDateToDefault(1); //some method
Bundle args = new Bundle();
args.putInt("day", day1);
args.putInt("month", month1);
args.putInt("year", year1);
myDatePickerDialogFragment d1 = new myDatePickerDialogFragment();
d1.setArguments(args);
d1.setOnDateSetListener(datePickerListener1); //onDateSet Listener that is not included in this question
d1.show(getFragmentManager(), "days");
return true;
}
return false;
}
});
我建议在 EditTexts 上将 onTouchListener
更改为 onClickListener
。背后的原因是触摸监听器会在每次事件发生时触发(例如手指按下、手指移动、手指向上等),而点击监听器只会触发一次。
所以在 DaysTabView
class 中,更改为:
mFromDate.setOnClickListener(new View.OnClickListener() {
@Override
public boolean onClick(View v) {
mFromDate.setText("");
SetDateToDefault(1); //some method
Bundle args = new Bundle();
args.putInt("day", day1);
args.putInt("month", month1);
args.putInt("year", year1);
myDatePickerDialogFragment d1 = new myDatePickerDialogFragment();
d1.setArguments(args);
d1.setOnDateSetListener(datePickerListener1); //onDateSet Listener that is not included in this question
d1.show(getFragmentManager(), "days");
return false;
}
});
更改您的 onTouchListener
代码
mFromDate.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
final int actionPeformed = ev.getAction();
switch(actionPeformed){
case MotionEvent.ACTION_UP:{
mFromDate.setText("");
SetDateToDefault(1); //some method
Bundle args = new Bundle();
args.putInt("day", day1);
args.putInt("month", month1);
args.putInt("year", year1);
myDatePickerDialogFragment d1 = new myDatePickerDialogFragment();
d1.setArguments(args);
d1.setOnDateSetListener(datePickerListener1); //onDateSet Listener that is not included in this question
d1.show(getFragmentManager(), "days");
break;
}
return true;
}
});
也许你应该使用 ClickListener 而不是 TouchListener