从 BottomSheetDialogFragment 获取值
Get value from BottomSheetDialogFragment
我有一个片段 (A),其中有 3 个动作可用。当用户 select 一个动作时,在此之前,我需要询问他用于登录的密码。我想使用 BottomSheetDialogFragment 来做到这一点,但我不知道如何让片段 A 在输入的密码正确的情况下进行监听。
这是 BottomSheetDialogFragment 的代码
class ConfirmPasswordBottomSheet : BottomSheetDialogFragment() {
companion object {
const val TAG = "ConfirmPasswordBottomSheet"
}
fun newInstance(): ConfirmPasswordBottomSheet {
return ConfirmPasswordBottomSheet()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setStyle(STYLE_NORMAL, R.style.CartissimaSheetDialogTheme)
}
@Inject
lateinit var sharedPreferences: SharedPreferences
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.bottom_confirm_pwd, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
showKeyboard()
confirm_pwd_btn.setOnClickListener {
if(DesEncrypted().encrypt(confirm_pwd_edit.text.toString()) == ServiceUtils.getPassword(sharedPreferences)){
}
}else{
with(requireActivity() as MainActivity) {
buildAlertMessageOneButton(
getString(R.string.common_warning),
getString(R.string.incorrect_password),
getString(R.string.common_close)
)
}
}
}
}
}
这是片段 A 中我需要检查密码的地方(在 2 个 if 之间)
show_pin_btn_float.setOnClickListener {
if (with(activity as MainActivity) { checkCardPermission(PinOptionFragment.card.authorities[AuthoritiesCardEnum.CARTE_VIEW_PIN]!!) }) {
if (card.driverPin) {
cardViewModel.requestPinDriver(with(activity as MainActivity) { currentUser.mainCode })
} else {
cardViewModel.requestPin(card.pan)
}
}
}
通过方法使用接口
public interface PasswordCheck {
fun isSuccess(password : Boolean);
}
在底部sheet构造函数中传递接口。
然后像这样在您的片段中调用它:
val callback = object : PasswordCheck {
override fun isSuccess(password : Boolean) {
if(password){
//do what you want
}
}
}
您必须为此任务使用界面。下面根据您提供的代码给出了一个简单的用例。
首先在ConfirmPasswordBottomSheet
中创建一个监听器,如:
class ConfirmPasswordBottomSheet : BottomSheetDialogFragment() {
private lateinit var listener: OnActionCompleteListener
fun setOnActionCompleteListener(listener: OnActionCompleteListener) {
this.listener = listener
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// your other code here
confirm_pwd_btn.setOnClickListener {
// pass the value of password here
listener.onActionComplete("your password is this")
}
}
// your other code here
interface OnActionCompleteListener {
fun onActionComplete(str: String)
}
}
然后在创建 ConfirmPasswordBottomSheet
的实例之后,您必须通过传递 OnActionCompleteListener
.
的实例来调用 setOnActionCompleteListener()
创建一个您想要使用它的实例,如下所示:
val listener = object: ConfirmPasswordBottomSheet.OnActionCompleteListener() {
fun onActionComplete(str: String) {
// do what you want to do here because this block will be invoked in bottom sheet
// you will receive "your password is this" here
// as per question maybe check your password / pin
if (with(activity as MainActivity) { checkCardPermission(PinOptionFragment.card.authorities[AuthoritiesCardEnum.CARTE_VIEW_PIN]!!) }) {
if (card.driverPin) {
cardViewModel.requestPinDriver(with(activity as MainActivity) { currentUser.mainCode })
} else {
cardViewModel.requestPin(card.pan)
}
}
}
}
创建接口实例后,您必须将其设置为片段 (A) 中 ConfirmPasswordBottomSheet
的实例,如下所示:
val bottomSheet = ConfirmPasswordBottomSheet()
bottomSheet.setOnActionCompleteListener(listener)
注意:此代码未经测试,可能需要一些修改,因为它不在我的脑海中。
我有一个片段 (A),其中有 3 个动作可用。当用户 select 一个动作时,在此之前,我需要询问他用于登录的密码。我想使用 BottomSheetDialogFragment 来做到这一点,但我不知道如何让片段 A 在输入的密码正确的情况下进行监听。
这是 BottomSheetDialogFragment 的代码
class ConfirmPasswordBottomSheet : BottomSheetDialogFragment() {
companion object {
const val TAG = "ConfirmPasswordBottomSheet"
}
fun newInstance(): ConfirmPasswordBottomSheet {
return ConfirmPasswordBottomSheet()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setStyle(STYLE_NORMAL, R.style.CartissimaSheetDialogTheme)
}
@Inject
lateinit var sharedPreferences: SharedPreferences
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.bottom_confirm_pwd, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
showKeyboard()
confirm_pwd_btn.setOnClickListener {
if(DesEncrypted().encrypt(confirm_pwd_edit.text.toString()) == ServiceUtils.getPassword(sharedPreferences)){
}
}else{
with(requireActivity() as MainActivity) {
buildAlertMessageOneButton(
getString(R.string.common_warning),
getString(R.string.incorrect_password),
getString(R.string.common_close)
)
}
}
}
}
}
这是片段 A 中我需要检查密码的地方(在 2 个 if 之间)
show_pin_btn_float.setOnClickListener {
if (with(activity as MainActivity) { checkCardPermission(PinOptionFragment.card.authorities[AuthoritiesCardEnum.CARTE_VIEW_PIN]!!) }) {
if (card.driverPin) {
cardViewModel.requestPinDriver(with(activity as MainActivity) { currentUser.mainCode })
} else {
cardViewModel.requestPin(card.pan)
}
}
}
通过方法使用接口
public interface PasswordCheck {
fun isSuccess(password : Boolean);
}
在底部sheet构造函数中传递接口。
然后像这样在您的片段中调用它:
val callback = object : PasswordCheck {
override fun isSuccess(password : Boolean) {
if(password){
//do what you want
}
}
}
您必须为此任务使用界面。下面根据您提供的代码给出了一个简单的用例。
首先在ConfirmPasswordBottomSheet
中创建一个监听器,如:
class ConfirmPasswordBottomSheet : BottomSheetDialogFragment() {
private lateinit var listener: OnActionCompleteListener
fun setOnActionCompleteListener(listener: OnActionCompleteListener) {
this.listener = listener
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// your other code here
confirm_pwd_btn.setOnClickListener {
// pass the value of password here
listener.onActionComplete("your password is this")
}
}
// your other code here
interface OnActionCompleteListener {
fun onActionComplete(str: String)
}
}
然后在创建 ConfirmPasswordBottomSheet
的实例之后,您必须通过传递 OnActionCompleteListener
.
setOnActionCompleteListener()
创建一个您想要使用它的实例,如下所示:
val listener = object: ConfirmPasswordBottomSheet.OnActionCompleteListener() {
fun onActionComplete(str: String) {
// do what you want to do here because this block will be invoked in bottom sheet
// you will receive "your password is this" here
// as per question maybe check your password / pin
if (with(activity as MainActivity) { checkCardPermission(PinOptionFragment.card.authorities[AuthoritiesCardEnum.CARTE_VIEW_PIN]!!) }) {
if (card.driverPin) {
cardViewModel.requestPinDriver(with(activity as MainActivity) { currentUser.mainCode })
} else {
cardViewModel.requestPin(card.pan)
}
}
}
}
创建接口实例后,您必须将其设置为片段 (A) 中 ConfirmPasswordBottomSheet
的实例,如下所示:
val bottomSheet = ConfirmPasswordBottomSheet()
bottomSheet.setOnActionCompleteListener(listener)
注意:此代码未经测试,可能需要一些修改,因为它不在我的脑海中。