我在一个片段中有一个相对的布局。该片段位于MainActivity内部。我想对其进行setonClickListener
事件以打开一个新活动。我想进行相对布局rlLogOut
来打开SignInActivity
活动。但是如何将setOnClickListener
事件放在片段上。是在OtherFragment.kt还是MainActivity中
我试图在MainActivity中制作setOnClickListener
,但我知道那不是解决方案。我认为目的是问题。但是我不知道它的语法。我是科特林的新人。
import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import kotlinx.android.synthetic.main.fragment_others.*
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
/**
* A simple [Fragment] subclass.
*
*/
class OtherFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
rlLogOut.setOnClickListener{
val mainIntent = Intent(
this@OtherFrament,
SignInActivity::class.java
)
startActivity(mainIntent)
}
return inflater.inflate(R.layout.fragment_others, container, false)
}
}
我希望单击rlLogOut
会打开SignInActivity
活动。感谢您的帮助。
答案 0 :(得分:0)
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View = inflater.inflate(R.layout.fragment_others, container, false)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
rlLogOut.setOnClickListener {
val mainIntent = Intent(requireActivity(), SignInActivity::class.java)
requireActivity().startActivity(mainIntent)
}
}