【问题标题】:button.setOnClickListener in fragment is not working片段中的 button.setOnClickListener 不起作用
【发布时间】:2019-03-04 17:45:15
【问题描述】:

这是我的布局 -> 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中没有错误。 我还没有找到问题,请帮忙?

编辑1

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

<?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()
}

}

【问题讨论】:

    标签: android button kotlin fragment onclicklistener


    【解决方案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()
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 2016-12-20
      相关资源
      最近更新 更多