gpt4 book ai didi

android - 在哪里可以添加 FindViewById() 和 fragment 中的监听器?

转载 作者:太空狗 更新时间:2023-10-29 16:11:47 26 4
gpt4 key购买 nike

我创建了一个 Fragment 并将其命名为 Kabar 我可以看到 ButtonTextView 出现了正如预期的那样。

这是我的 fragment :

public class Kabar extends Fragment   {
private Button button;
private TextView text;

public Kabar() {
// Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v= inflater.inflate(R.layout.fragment_kabar , container, false);
return v;
}
}

这是我的 fragment_kabar 布局:

 <FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.examplee.my.Kabar">

<!-- TODO: Update blank fragment layout -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="mohsen kabar" />

<Button
android:id="@+id/tt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="113dp"
android:text="Button"
android:layout_below="@+id/mohsenkabar"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="93dp"
android:layout_marginEnd="93dp" />
</RelativeLayout>

我需要使用 ButtonTextView

我在哪里可以放置此代码?

text=(TextView) text.findViewById(R.id.tt1);
button=(Button) button.findViewById(R.id.tt2);
button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
tt1.setText("kkk mmm bbb ddd");
}
});

最佳答案

有两个选项 onCreateViewonViewCreated (首选)方法。

void onViewCreated (View view, Bundle savedInstanceState)

Called immediately after onCreateView(LayoutInflater, ViewGroup, Bundle) has returned, but before any saved state has been restored in to the view. This gives subclasses a chance to initialize themselves once they know their view hierarchy has been completely created. The fragment's view hierarchy is not however attached to its parent at this point.

关于android - 在哪里可以添加 FindViewById() 和 fragment 中的监听器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45372645/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com