在哪里可以添加 FindViewById() 和片段中的侦听器?

Where can I add the FindViewById() and the listeners in a fragment?

我创建了一个 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");
    }
});

尝试将此代码添加到您的 kabar 片段中

  @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);

      TextView  text=(TextView) v.findViewById(R.id.tt1);
      TextView  button=(TextView) v.findViewById(R.id.tt2);

      button.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
      text.setText("kkk mmm bbb ddd");
       }
   });
    return v;
}

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

void onViewCreated(View视图, 捆绑 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.

将您的代码替换为 onCreateView

中的以下代码
text  =(TextView) v.findViewById(R.id.tt1); 
button=(Button) v.findViewById(R.id.tt2); 
button.setOnClickListener(new View.OnClickListener() { 

    @Override 
    public void onClick(View v) {
        text.setText("kkk mmmm bbbb ddd"); 
    } 
}); 

在您的代码中进行此更改

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

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

您应该在您的 Kabar OnCreateView 中添加您的代码 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);

        TextView  text = (TextView) v.findViewById(R.id.tt1);
        Button button = (Button) v.findViewById(R.id.tt2);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                text.setText("kkk mmm bbb ddd");
            }
        });
        return v;
    }
}