使用 Kotlin 从片段 A 调用片段 B 的方法



目前我正在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(" ")
}

}

我的问题是如何将getInstance((方法从Kotlin类调用到java类。由于在 Kotlin 中不允许使用静态。

> Kotlin 已将 static 替换为 object 和 comapnion 对象

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

像下面一样

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 @JvmStatic注释的配套对象方法:

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

最新更新