我点击按钮时会显示底部工作表对话框,所以我已经将底部工作表对话框的所有逻辑实现为按钮的onClick方法,如何创建单独的类来显示底部工作表对话框并在onClick中调用方法
这是我在onClick中的代码:
BottomSheetDialog mBottomSheetDialog = new BottomSheetDialog(this);
View sheetView = getLayoutInflater().inflate(R.layout.bottom_sheet, null);
NumberPicker minutePicker = (NumberPicker) sheetView.findViewById(R.id.np);
tv = (TextView) sheetView.findViewById(R.id.tv);
minutePicker.setMaxValue(100);
minutePicker.setMinValue(0);
minutePicker.setWrapSelectorWheel(false);
mBottomSheetDialog.setContentView(sheetView);
mBottomSheetDialog.show();
minutePicker.setOnValueChangedListener(
new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal,
int newVal) {
tv.setText(Integer.toString(newVal));
}
});
}
答案 0 :(得分:0)
实现关注点分离并使代码模块化
public class ClsBottomSheet{
public static TextView tv;
LayoutInflater inflater;
BottomSheetDialog mBottomSheetDialog;
public static NumberPicker minutePicker;
public ClsBottomSheet(Context context) {
mBottomSheetDialog = new BottomSheetDialog(context);
inflater = LayoutInflater.from(context);
}
public BottomSheetDialog showDialog(){
View sheetView = inflater.inflate(R.layout.bottom_sheet, null);
minutePicker = (NumberPicker) sheetView.findViewById(R.id.np);
tv = (TextView) sheetView.findViewById(R.id.tv);
Button btnOne = (Button) sheetView.findViewById(R.id.btn_one);
Button btnTwo = (Button) sheetView.findViewById(R.id.btn_two);
btnOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mBottomSheetDialog.cancel();
}
});
btnTwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mBottomSheetDialog.dismiss();
}
});
minutePicker.setMaxValue(100);
minutePicker.setMinValue(0);
minutePicker.setWrapSelectorWheel(false);
mBottomSheetDialog.setContentView(sheetView);
minutePicker.setOnValueChangedListener(new ClsCallback());
return mBottomSheetDialog;
}
}