我有一个选项卡式活动,其中包含两个底部选项卡。框架布局充满了我创建的特殊布局文件。这两个布局文件由扩展片段类的 java 类运行。这是我对我现在所拥有的内容的简要描述。
现在来谈谈一个问题的细节。布局文件包含两个 EditText 字段,单击这些字段时必须创建 DatePickerDialog。我已经使用 DialogFragment 类实现了对话框,一个用于两个文本字段。Hovewer,当我单击编辑文本时,它们会生成多个 DatePicker 对话框,而不是一个,目前尚不清楚背后的逻辑是什么。它可以生成 2、3 甚至 4 个对话窗口。
如何解决此问题?这是我的代码:
活动
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);
}
}
选项卡视图类之一
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;
}
});
}
}
和对话片段类
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);
}
}
我建议在EditTexts上用onClickListener
来更改onTouchListener
。背后的原因是每次发生事件(例如手指向下、手指移动、手指向上等)都会触发触摸侦听器,而单击侦听器只会触发一次。
所以在DaysTabView
类中,改成这个:
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;
}
});
在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;
}
});
更改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