猿问

如何在 Kotlin 中使用片段内的按钮进行 Android 开发?

我是 Android 开发新手,并从 Android Studio 创建了一个新项目,其中包含 Kotlin 中的底部导航活动。此外MainActivity.kt,还生成了仪表板、主页和通知片段及其 ViewModel。当我处理 MainActivity 类中的按钮单击时,一切正常。


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

        }

    }

}

这是工作按钮的屏幕截图 按钮工作正常

但是我不明白如何使用不同片段内的按钮。我尝试为仪表板片段第二个按钮的屏幕截图中的第二个按钮创建功能,但我还没有找到解决方案。我努力了


HUWWW
浏览 181回答 3
3回答

慕少森

这就是我最终的结果:class DashboardFragment : Fragment() {private lateinit var dashboardViewModel: DashboardViewModeloverride fun onCreateView(&nbsp; &nbsp; inflater: LayoutInflater,&nbsp; &nbsp; container: ViewGroup?,&nbsp; &nbsp; savedInstanceState: Bundle?): View? {&nbsp; &nbsp; dashboardViewModel =&nbsp; &nbsp; &nbsp; &nbsp; ViewModelProviders.of(this).get(DashboardViewModel::class.java)&nbsp; &nbsp; val root = inflater.inflate(R.layout.fragment_dashboard, container, false)&nbsp; &nbsp; val textView: TextView = root.findViewById(R.id.text_dashboard)&nbsp; &nbsp; dashboardViewModel.text.observe(this, Observer {&nbsp; &nbsp; &nbsp; &nbsp; textView.text = it&nbsp; &nbsp; })&nbsp; &nbsp; val button2 : Button = root.findViewById<Button>(R.id.temporary_button2)&nbsp; &nbsp; button2.setOnClickListener{&nbsp; &nbsp; &nbsp; &nbsp; println("clicked button 2")&nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(view?.context, "Button Clicked", Toast.LENGTH_LONG).show()&nbsp; &nbsp; }&nbsp; &nbsp; return root}}

月关宝盒

片段中没有findViewById,通常在片段内您应该重写该onCreateView方法,膨胀您自己的布局并尝试从您膨胀的视图中获取视图。例如 :&nbsp; &nbsp; @Nullable&nbsp; &nbsp; @Override&nbsp; &nbsp; public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; if (mContentView == null){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mContentView = inflater.inflate(R.layout.family_fragment_scene_list, container, false);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; type = getArguments().getInt(ARGS_KEY_TYPE);&nbsp; &nbsp; &nbsp; &nbsp; adapter = new SceneAdapter(getContext());&nbsp; &nbsp; &nbsp; &nbsp; // get views from mContentView&nbsp; &nbsp; &nbsp; &nbsp; mSceneList = (RecyclerView) mContentView.findViewById(R.id.swipe_target);&nbsp; &nbsp; &nbsp; &nbsp; mSceneList.setLayoutManager(new LinearLayoutManager(getContext()));&nbsp; &nbsp; &nbsp; &nbsp; mSceneList.setAdapter(adapter);&nbsp; &nbsp; &nbsp; &nbsp; return mContentView;&nbsp; &nbsp; }

神不在的星期二

使用 getView()/view 尝试内部 onViewCreated 而不是 onCreateView。例如。:override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {&nbsp; &nbsp;val temporary_button = getView().findViewById<Button>(R.id.temporary_button2)&nbsp; &nbsp;temporary_button2.setOnClickListener{&nbsp; &nbsp;makeText(this, "You clicked the button", LENGTH_LONG).show()&nbsp; &nbsp;}&nbsp;}
随时随地看视频慕课网APP

相关分类

Java
我要回答