目前,我在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类到Java类调用getInstance()方法。因为Kotlin中不允许使用Static。
答案 0 :(得分:2)
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
}
}
}
答案 1 :(得分:2)
科特林(Kotlin)已用object和comapnion对象替换了static
您可以在类内部的伴侣对象中将所需的内容定义为静态。
如下所示
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()