片段中的button.setOnClickListener无法正常工作

时间:2018-09-28 17:31:22

标签: android button kotlin fragment onclicklistener

这是我的布局-> fragment_three.xml

<ScrollView
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/supportback">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        //some Textviews here...
          <Button
            android:id="@+id/sendButton"
            android:layout_width="58dp"
            android:layout_height="51dp"
            android:layout_gravity="right"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:clickable="true"
            android:background="@drawable/send_email"/>
    </LinearLayout>
</ScrollView>

这是我的片段:

class fragmentC : Fragment()  {
companion object {
    fun newInstance():Fragment {
        var fb :fragmentC = fragmentC()
        return fb 
}}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    val rootView = inflater.inflate(R.layout.fragment_three,container,false)
    val btn = rootView.findViewById(R.id.sendButton) as Button
    btn.setOnClickListener {
        Toast.makeText(context,"test",Toast.LENGTH_LONG).show()
    }
    return rootView
}}

没有错误,没有异常,logcat中没有错误。 我仍然找不到问题,请帮忙吗?

edit1

这是片段应在其中工作的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity"
android:id="@+id/activity_main">

<FrameLayout
    android:id="@+id/frame_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/navigation"
    android:animateLayoutChanges="true">
</FrameLayout>

<android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="#389486"
    app:menu="@menu/bottom_view">
</android.support.design.widget.BottomNavigationView>
</RelativeLayout>

P.S:还有两个其他片段,但是现在我还没有触摸它是空白的

edit2 主要活动课

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
        navigation.setOnNavigationItemSelectedListener (object : BottomNavigationView.OnNavigationItemSelectedListener{
            override fun onNavigationItemSelected(item: MenuItem): Boolean {
                when(item.itemId){
                    R.id.bot_list ->{
                    drawerLayout.openDrawer(Gravity.START)
                    }
                    R.id.bot_home ->{
                            var ft : FragmentTransaction = supportFragmentManager.beginTransaction()
                            ft.replace(R.id.frame_layout,fragmentA.newInstance())
                            ft.commit()
                    }
                    R.id.bot_cart ->{
                        var ft : FragmentTransaction = supportFragmentManager.beginTransaction()
                            ft.replace(R.id.frame_layout,fragmentB.newInstance())
                            ft.commit() }
                    R.id.bot_message ->{
                        //here is the required fragment
                        var ft : FragmentTransaction = supportFragmentManager.beginTransaction()
                        ft.replace(R.id.frame_layout,fragmentC.newInstance())
                        ft.commit() }
                }
                return true
            }
        })
    var ft : FragmentTransaction = supportFragmentManager.beginTransaction()
    ft.replace(R.id.frame_layout,fragmentA.newInstance())
    ft.commit()
}

}

1 个答案:

答案 0 :(得分:1)

我不知道是什么原因引起的,但是像下面这样修改代码即可解决

class fragmentC : Fragment(),View.OnClickListener  {
companion object {
    fun newInstance():Fragment {
        var fb :fragmentC = fragmentC()
        return fb
    }

}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    val rootView = inflater.inflate(R.layout.fragment_three, container, false)
    val location: Button = rootView.findViewById(R.id.sendButton)
    location.setOnClickListener(this)
    return rootView
}

override fun onClick(v: View) {
    Toast.makeText(context,"test",Toast.LENGTH_LONG).show()
}
}