亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 Kotlin 中使用片段內的按鈕進行 Android 開發?

如何在 Kotlin 中使用片段內的按鈕進行 Android 開發?

HUWWW 2023-08-23 15:06:46
我是 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()        }    }}這是工作按鈕的屏幕截圖 按鈕工作正常但是我不明白如何使用不同片段內的按鈕。我嘗試為儀表板片段第二個按鈕的屏幕截圖中的第二個按鈕創建功能,但我還沒有找到解決方案。我努力了
查看完整描述

3 回答

?
慕少森

TA貢獻2019條經驗 獲得超9個贊

這就是我最終的結果:


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

}

}


查看完整回答
反對 回復 2023-08-23
?
月關寶盒

TA貢獻1772條經驗 獲得超5個贊

片段中沒有findViewById,通常在片段內您應該重寫該onCreateView方法,膨脹您自己的布局并嘗試從您膨脹的視圖中獲取視圖。例如 :


    @Nullable

    @Override

    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        if (mContentView == null){

            mContentView = inflater.inflate(R.layout.family_fragment_scene_list, container, false);

        }


        type = getArguments().getInt(ARGS_KEY_TYPE);


        adapter = new SceneAdapter(getContext());

        // get views from mContentView

        mSceneList = (RecyclerView) mContentView.findViewById(R.id.swipe_target);

        mSceneList.setLayoutManager(new LinearLayoutManager(getContext()));

        mSceneList.setAdapter(adapter);


        return mContentView;

    }


查看完整回答
反對 回復 2023-08-23
?
神不在的星期二

TA貢獻1963條經驗 獲得超6個贊

使用 getView()/view 嘗試內部 onViewCreated 而不是 onCreateView。


例如。:


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

   }

 }



查看完整回答
反對 回復 2023-08-23
  • 3 回答
  • 0 關注
  • 226 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號