2017-06-13 66 views
0

我有底部表單對話框,當按鈕被點擊時顯示,所以我已經實現了底部表單對話框的所有邏輯到onClick方法的按鈕,我怎樣才能使單獨的類顯示底部表單對話框,只需調用方法的onClickAndroid:如何使BottomSheetDialog的分離類

這是我在的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

要實現相關分離,使你的代碼模塊化

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; 
} 
}