首页 > 解决方案 > KOTLIN - 如何将 TextViews 和按钮设置从 Activity 设置为片段

问题描述

我是 Android 新手,尤其是 Kotlin 开发。

从标题来看,我试图了解如何实现这一点:我有一个带有一些按钮和文本视图的活动。我将实现在 UI 上单击 5 次后打开的隐藏片段。该片段工作看起来像活动。我能够正确打开片段并正确设置布局。我不知道如何将按钮活动设置从活动替换为片段。我对文本视图有同样的问题。我怎样才能实现它?提前致谢。

这里 Activity Kotlin 部分打开片段:

override fun onTouchEvent(event: MotionEvent): Boolean {
        var eventaction = event.getAction()
        if (eventaction == MotionEvent.ACTION_UP) {

            //get system current milliseconds
            var time = System.currentTimeMillis()


            //if it is the first time, or if it has been more than 3 seconds since the first tap ( so it is like a new try), we reset everything
            if (startMillis == 0L || (time-startMillis> 3000) ) {
                startMillis=time
                count=1
            }

            //it is not the first, and it has been  less than 3 seconds since the first
            else{ //  time-startMillis< 3000
                count++
            }

            if (count==5) { 

//            Log.d("tag","start hidden layout")

                // Get the text fragment instance
                val textFragment = MyFragment()

                val mytostring =board_status_tv.toString()
                val mArgs = Bundle()
                mArgs.putString(BOARDSTATE, mytostring)

                textFragment.setArguments(mArgs)


                // Get the support fragment manager instance
                val manager = supportFragmentManager

                // Begin the fragment transition using support fragment manager
                val transaction = manager.beginTransaction()

                // Replace the fragment on container
                transaction.replace(R.id.fragment_container,textFragment)
                transaction.addToBackStack(null)
                // Finishing the transition
                transaction.commit()



            }
            return true
        }
        return false

    }

片段 Kotlin 类:

class MyFragment : Fragment(){
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val parentViewGroup = linearLayout
        parentViewGroup?.removeAllViews()


    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        // Get the custom view for this fragment layout
        val view = inflater!!.inflate(R.layout.my_own_fragment,container,false)

        // Get the text view widget reference from custom layout
        val tv = view.findViewById<TextView>(R.id.text_view)
//        val tv1 = view.findViewById<TextView>(R.id.board_status_tv1)




        // Set a click listener for text view object
        tv.setOnClickListener{
            // Change the text color
            tv.setTextColor(Color.RED)

            // Show click confirmation
            Toast.makeText(view.context,"TextView clicked.",Toast.LENGTH_SHORT).show()
        }

        // Return the fragment view/layout
        return view
    }

    override fun onPause() {
        super.onPause()
    }

    override fun onAttach(context: Context?) {
        super.onAttach(context)
    }

    override fun onDestroy() {
        super.onDestroy()
    }

    override fun onDetach() {
        super.onDetach()
    }

    override fun onStart() {
        super.onStart()
    }

    override fun onStop() {
        super.onStop()
    }
}

标签: androidandroid-fragmentskotlin

解决方案


请注意,在将文本转换为字符串之前,您需要先获取文本,就像第二行中的那样。

board_status_tv .getText()。toString()

 val textFragment = MyFragment()
 val mytostring = board_status_tv.getText().toString()
 val mArgs = Bundle()
 mArgs.putString(BOARDSTATE, mytostring)
 textFragment.setArguments(mArgs)

希望这能解决您的问题


推荐阅读