如何在Kotlin中为TextInputField设置OnClickListener

时间:2018-07-24 18:33:06

标签: android android-studio kotlin

当用户单击文本输入字段时,我试图在活动中(使用easyPermissions)请求权限。我当前的代码如下:

class PageFragment : Fragment(), EasyPermissions.PermissionCallbacks {

    private lateinit var viewModel: PageViewModel
    private lateinit var binding: PageBinding
    private lateinit var interpreter: PageInterpreter
    private val messagesCoordinator: MessageCoordinator by lazy {
        MessageCoordinator(viewModel, interpreter)
    }

    companion object {
        private const val RC_READ_PERMISSIONS = 102
        @JvmStatic
        fun newInstance() = PageFragment().apply {
            arguments = Bundle().apply {
            }
        }
    }

    interface PageCallback {
        fun goBack()
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_page, container, false)
        return binding.root
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)

        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this)
    }

    @AfterPermissionGranted(RC_READ_PERMISSIONS)
    private fun getPermissions() {
        Log.i("Permission asked","Get Permission function activated")
        context?.let {
            if (EasyPermissions.hasPermissions(it, android.Manifest.permission.READ_CONTACTS)) {
                //TODO
            } else {
                EasyPermissions.requestPermissions(this, it.getString(R.string.fragment_page_explanation), RC_READ_PERMISSIONS,
                    android.Manifest.permission.READ_CONTACTS)
            }
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        Log.i("View Created","View has been created")
        editName.setOnClickListener {
            Log.i("Name","on click listener activated")
            this.getPermissions()
        }
    }

    override fun onPermissionsGranted(requestCode: Int, perms: MutableList<String>) {
        //TODO
    }
    override fun onPermissionsDenied(requestCode: Int, perms: MutableList<String>) {
        //TODO
    }
}

基本上,我在片段布局上有6个输入字段,我希望询问是否允许用户单击任何文本输入字段的权限。在这里,我只尝试一个。

谢谢

Feras A.

1 个答案:

答案 0 :(得分:1)

据我了解,如果您确定所有EditText的行为均相同,则可以创建clickListener的单个实例,并将其设置为EditText。

    val clickListener = View.OnClickListener {
        Log.i("Name","on click listener activated")
        this@PageFragment.getPermissions()
    }

    editName.setOnClickListener(clickListener)
    editSomethingElse.setOnClickListener(clickListener)
    editSurname.setOnClickListener(clickListener)

甚至创建类似EditText的列表,并使用以下命令设置侦听器:

    listOf(editName,  editSomethingElse, editSurname).map {
        it.setOnClickListener(clickListener)
    }

编辑 可能是this吗?还有一个相关的topic