片段中的 button.setOnClickListener 不起作用
Posted
技术标签:
【中文标题】片段中的 button.setOnClickListener 不起作用【英文标题】:button.setOnClickListener in fragment is not working 【发布时间】: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_
android:layout_
android:background="@drawable/supportback">
<LinearLayout
android:layout_
android:layout_
android:orientation="vertical">
//some Textviews here...
<Button
android:id="@+id/sendButton"
android:layout_
android:layout_
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_
android:layout_
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_
android:layout_
android:layout_above="@+id/navigation"
android:animateLayoutChanges="true">
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_
android:layout_
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】:我不知道是什么导致了问题,但是修改下面的代码可以解决它
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()
【讨论】:
以上是关于片段中的 button.setOnClickListener 不起作用的主要内容,如果未能解决你的问题,请参考以下文章