java - 使用 Kotlin 从 Fragment A 调用 Fragment B 的方法
问题描述
目前我正在java环境中工作并尝试开始使用kotlin。我的 First class 是 Java 类型,下一个 class 是 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 已将 static 替换为 object 和 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()
推荐阅读
- powershell - Powershell 中的“TABLE_NOT_AVAILABLE”.net SAP 连接器
- .net - 为什么是 Enumerable.OfType
(IEnumerable) 不能按预期使用 ValueTuple? - php - Laravel - 将数据从中间件发送到路由
- postgresql - PostgreSQL - 在多个分区表上创建索引
- reactjs - 正确使用反应状态和路由
- python - 如何在sqlalchemy中加入多个表
- batch-file - .bat 文件中的特殊字符
- r - 无法在 r 中加载函数 install_tensorflow()
- javascript - 如何知道两个元素的 z-index
- java - Sparql 查询适用于 wikidata 端点,但不适用于 JENA 界面