gpt4 book ai didi

java - 使用 Kotlin 从 fragment A 调用 fragment B 的方法

转载 作者:行者123 更新时间:2023-12-01 16:45:32 25 4
gpt4 key购买 nike

目前我正在java环境中工作并尝试开始使用kotlin。我的第一个类是 Java 类型,下一个类是 Kotlin 类型。我的第一个类如下

public class FragmentDashboard extends BaseFragment {
Button btnLaunchComplaint;
TextView tvSupport;

public static FragmentDashboard getInstance(Bundle bundle, String title, int icon) {
FragmentDashboard fragment = new FragmentDashboard();
fragment.setArguments(bundle);
fragment.setFragmentTitle(title);
fragment.setFragmentIconId(icon);
return fragment;
}

@Override
protected void initializeControls(View v) {
btnLaunchComplaint = v.findViewById(R.id.btnLaunchComplaint);
tvSupport = v.findViewById(R.id.tvSupport);

}

@Override
protected int getLayoutResourceId() {
return R.layout.fragment_dashborad_layout;
}

@Override
protected void initializationBundle(Bundle bundle) {

}

@Override
protected void attachListeners() {
btnLaunchComplaint.setOnClickListener(this);
tvSupport.setOnClickListener(this);
}

@Override
protected void initializeData() {
animateViews();
}

@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnLaunchComplaint:
FragmentForm fragmentForm = FragmentForm.getInstance(new Bundle(), "", -1);
replaceFragment(fragmentForm, false, false, true, "");
break;
case R.id.tvSupport:
FragmentSupport fragmentSupport = FragmentSupport.getInstance(new Bundle(), "", -1);
replaceFragment(fragmentSupport, false, false, true, "");
break;


}
super.onClick(view);
}

@Override
public void onResume() {
super.onResume();
setNavigationTitle(getResources().getString(R.string.wasa_home));


}

private void animateViews() {
Animation animateTopDown = AnimationUtils.loadAnimation(getActivity(), R.anim.left_in);
btnLaunchComplaint.startAnimation(animateTopDown);
}
}

我的 Kotlin 类代码

class FragmentRegisterComplaint : BaseFragment() {

private var etComplainantName: EditText? = null
private var etBillAccountNo: EditText? = null
private var etAmountPayable: EditText? = null
private var etDueDate: EditText? = null
private var etArrears: EditText? = null
private var etMobile: EditText? = null
private var etPhone: EditText? = null
private var etAddress: EditText? = null
private var etComplaintType: EditText? = null
private var etComplaintSubType: EditText? = null
private var etTown: EditText? = null
private var etSubDivision: EditText? = null
private var etComplainantComments: EditText? = null
private var btnSubmit: Button? = null
private var btnCancel: Button? = null
private var btnIssuePicture: ImageView? = null
private val options: DisplayImageOptions? = null
private val etTownSelectedId = -1
private val etSubDivisionSelectedId = -1
private val etComplaintTypeSelectedId = -1
private val etComplaintSubTypeSelectedId = -1
private val relevencyId = -1
private val priorityId = -1
private val sourceId = -1
fun getInstance(bundle: Bundle, title: String, icon: Int): FragmentRegisterComplaint {
val fragment = FragmentRegisterComplaint()
fragment.arguments = bundle
fragment.setFragmentTitle(title)
fragment.setFragmentIconId(icon)
return fragment
}

private val isValidFields: Boolean
get() {
var value = 0
if (etComplainantName!!.text.length < 1) {
setError(etComplainantName, resources.getString(R.string.enter_complainant_name))
value = 1
}
if (etBillAccountNo!!.text.length < 1) {
setError(etBillAccountNo, resources.getString(R.string.enter_account_no))
value = 1
}
if (isMobileEmpty(etMobile)) {
setError(etMobile, resources.getString(R.string.enter_phone_no))
value = 1
}
if (etComplaintTypeSelectedId < 0) {
setError(etComplaintType, resources.getString(R.string.select_complaint_type))
value = 1
}
if (etComplaintSubTypeSelectedId < 0) {
setError(etComplaintSubType, resources.getString(R.string.select_complaint_sub_type))
value = 1
}
if (etTownSelectedId < 0) {
setError(etTown, resources.getString(R.string.select_town))
value = 1
}
if (etSubDivisionSelectedId < 0) {
setError(etSubDivision, resources.getString(R.string.select_sub_division))
value = 1
}

return value == 0
}

override fun initializeControls(v: View) {
etComplainantName = v.findViewById(R.id.etComplainantName)
etBillAccountNo = v.findViewById(R.id.etBillAccountNo)
etAmountPayable = v.findViewById(R.id.etAmountPayable)
etDueDate = v.findViewById(R.id.etDueDate)
etArrears = v.findViewById(R.id.etArrears)
etMobile = v.findViewById(R.id.etMobile)
etPhone = v.findViewById(R.id.etPhoneNo)
etAddress = v.findViewById(R.id.etAddress)
etComplaintType = v.findViewById(R.id.etComplaintType)
etComplaintSubType = v.findViewById(R.id.etComplaintSubType)
etTown = v.findViewById(R.id.etTown)
etSubDivision = v.findViewById(R.id.etSubDivision)
etComplainantComments = v.findViewById(R.id.etComplainantComments)
btnSubmit = v.findViewById(R.id.btnSubmit)
btnCancel = v.findViewById(R.id.btnCancel)
btnIssuePicture = v.findViewById(R.id.btnIssuePicture)

}

override fun getLayoutResourceId(): Int {
return R.layout.fragment_register_complaint_layout
}

override fun initializationBundle(bundle: Bundle) {


}

override fun attachListeners() {

}

override fun initializeData() {

}

override fun isMobileEmpty(editText: EditText?): Boolean {
val strMobile = editText!!.text.toString()
val mobileArray = strMobile.split("-".toRegex()).dropLastWhile({ it.isEmpty() }).toTypedArray()

return mobileArray[0].contains(" ") || mobileArray[1].contains(" ")
}


}

我的问题是如何从 Kotlin 类调用 getInstance() 方法到 java 类。因为 Kotlin 中不允许静态。

最佳答案

Kotlin has replaced static with object and comapnion object

您可以在类内的伴生对象中将所需的内容定义为静态。

如下所示

companion object {
fun getInstance(bundle: Bundle, title: String, icon: Int): FragmentRegisterComplaint {
val fragment = FragmentRegisterComplaint()
fragment.arguments = bundle
fragment.setFragmentTitle(title)
fragment.setFragmentIconId(icon)
return fragment
}
}
}

现在在你的java类中你可以使用它作为

YorFragmentName.companion.method()

关于java - 使用 Kotlin 从 fragment A 调用 fragment B 的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52217017/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com