如何从另一个活动访问片段中的元素

时间:2019-01-05 19:32:34

标签: android-fragments kotlin android-fragmentactivity

我有一个片段,该片段有一个按钮(位于fragment_call.xml中),由“简单调用活动”调用。

class SimpleCallActivity : CallFragment.OnCallEvents {
override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        DataBindingUtil.setContentView<ActivitySimpleCallBinding>(this, R.layout.activity_simple_call).also {
            binding = it
        }

Activity_simple_call

<FrameLayout
        android:id="@+id/call_fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

fragment_call

<android.support.design.widget.FloatingActionButton
            android:id="@+id/button_call_disconnect"/>

CallFragment类

class CallFragment : Fragment() {
interface OnCallEvents {
    fun onCallHangUp()
    fun onToggleMic(): Boolean
} 
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? =
    DataBindingUtil.inflate<FragmentCallBinding>(inflater, R.layout.fragment_call, container, false).also {

    ...

    binding.buttonCallDisconnect.setOnClickListener({

        callEvents.onCallHangUp()
    })
}

如何从另一个类访问此按钮元素?当我直接使用

访问它时,它显示为null
import kotlinx.android.synthetic.main.fragment_call.*

button_call_disconnect.callOnClick();

0 个答案:

没有答案