Java 如何使用Kotlin中用于android开发的片段中的按钮?
我是android开发新手,在android studio中创建了一个新项目,在Kotlin中有一个底部导航活动。除了MainActivity.kt之外,还生成了dashboard、home和notifications片段及其ViewModels。当我在MainActivity类中处理按钮单击时,一切正常Java 如何使用Kotlin中用于android开发的片段中的按钮?,java,android-fragments,kotlin,Java,Android Fragments,Kotlin,我是android开发新手,在android studio中创建了一个新项目,在Kotlin中有一个底部导航活动。除了MainActivity.kt之外,还生成了dashboard、home和notifications片段及其ViewModels。当我在MainActivity类中处理按钮单击时,一切正常 class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val navView: BottomNavigationView = findViewById(R.id.nav_view)
val navController = findNavController(R.id.nav_host_fragment)
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
val appBarConfiguration = AppBarConfiguration(setOf(
R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications))
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
//handle button click
val temporary_button = findViewById<Button>(R.id.temporary_button)
temporary_button.setOnClickListener{
makeText(this, "You clicked the button", LENGTH_LONG).show()
}
}
}
我尝试使用如下函数:
class DashboardFragment : Fragment() {
private lateinit var dashboardViewModel: DashboardViewModel
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
dashboardViewModel =
ViewModelProviders.of(this).get(DashboardViewModel::class.java)
val root = inflater.inflate(R.layout.fragment_dashboard, container, false)
val textView: TextView = root.findViewById(R.id.text_dashboard)
dashboardViewModel.text.observe(this, Observer {
textView.text = it
})
return root
}
fun button2click (view: View){
println("Button clicked")
}
}
但是这种方式不起作用,当我点击按钮时应用程序崩溃
任何关于如何使用片段内部按钮的帮助都将被告知。请尝试使用getView/view创建内部的onViewCreated,而不是onCreateView e、 g: 片段中没有findViewById,通常在片段内部,您应该覆盖onCreateView方法,膨胀您自己的布局,并尝试从膨胀的视图中获取视图。例如: @可空 @凌驾 公众观点onCreateView@NonNull布局扁平充气机、@Nullable ViewGroup容器、@Nullable Bundle savedInstanceState{ 如果mContentView==null{ mContentView=inflater.inflater.layout.family\u fragment\u scene\u列表,容器,false; } type=getArguments.getIntARGS\u KEY\u type; adapter=newscenedaptergetcontext; //从mContentView获取视图 mSceneList=RecyclerView mContentView.findviewbydr.id.swipe\u目标; mSceneList.setLayoutManagerNewLinearLayoutManagerTargetContext; mSceneList.setAdapteradapter; 返回mContentView; }
这就是我最终的结果:
class DashboardFragment : Fragment() {
private lateinit var dashboardViewModel: DashboardViewModel
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
dashboardViewModel =
ViewModelProviders.of(this).get(DashboardViewModel::class.java)
val root = inflater.inflate(R.layout.fragment_dashboard, container, false)
val textView: TextView = root.findViewById(R.id.text_dashboard)
dashboardViewModel.text.observe(this, Observer {
textView.text = it
})
val button2 : Button = root.findViewById<Button>(R.id.temporary_button2)
button2.setOnClickListener{
println("clicked button 2")
Toast.makeText(view?.context, "Button Clicked", Toast.LENGTH_LONG).show()
}
return root
}
}
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/text_dashboard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<Button
android:id="@+id/temporary_button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="192dp"
android:layout_marginBottom="248dp"
android:onClick="button2click"
android:text="Button2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent" />
class DashboardFragment : Fragment() {
private lateinit var dashboardViewModel: DashboardViewModel
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
dashboardViewModel =
ViewModelProviders.of(this).get(DashboardViewModel::class.java)
val root = inflater.inflate(R.layout.fragment_dashboard, container, false)
val textView: TextView = root.findViewById(R.id.text_dashboard)
dashboardViewModel.text.observe(this, Observer {
textView.text = it
})
return root
}
fun button2click (view: View){
println("Button clicked")
}
}
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
val temporary_button = getView().findViewById<Button>(R.id.temporary_button2)
temporary_button2.setOnClickListener{
makeText(this, "You clicked the button", LENGTH_LONG).show()
}
}
class DashboardFragment : Fragment() {
private lateinit var dashboardViewModel: DashboardViewModel
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
dashboardViewModel =
ViewModelProviders.of(this).get(DashboardViewModel::class.java)
val root = inflater.inflate(R.layout.fragment_dashboard, container, false)
val textView: TextView = root.findViewById(R.id.text_dashboard)
dashboardViewModel.text.observe(this, Observer {
textView.text = it
})
val button2 : Button = root.findViewById<Button>(R.id.temporary_button2)
button2.setOnClickListener{
println("clicked button 2")
Toast.makeText(view?.context, "Button Clicked", Toast.LENGTH_LONG).show()
}
return root
}