KOTLIN-如何将TextViews和按钮设置从“活动”设置为片段

时间:2019-02-27 10:24:59

标签: android android-fragments kotlin

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

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

在这里打开片段的Activity Kotlin部分:

grpc

科特林片段课:

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

    }

1 个答案:

答案 0 :(得分:1)

请注意,您需要先获取Text,然后再将其转换为字符串,如第二行所示。

board_status_tv .getText()。 toString()

DataIntegrityViolationException

希望这可以解决您的问题