setOnClickListener 不适用于膨胀的布局

问题描述

我在子布局中制作的按钮不起作用。 我希望我的 button_sub 在我点击时工作。 我认为layer inflater有问题,但我还是不明白

final View addedView =inflater.inflate(R.layout.sublayout,null);

如何修改我的代码

这是我的 MainActivity 类

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    button3by3 = findViewById(R.id.button_3by3);
    container=findViewById(R.id.container);

    button3by3.setonClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            LayoutInflater inflater =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final View addedView =inflater.inflate(R.layout.sublayout,null);
            container.addView(addedView);
            
        }
    });
}

这是我的主要活动xml

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:layout_editor_absoluteX="154dp"
    tools:layout_editor_absoluteY="138dp">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Toast"
        android:id="@+id/button_sub"/>

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />
</LinearLayout>

最后是我的子布局活动:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sublayout);

    btn_sub=findViewById(R.id.button_sub);
    textView = findViewById(R.id.textView);
    btn_sub.setonClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(subAction.this,"Hello",Toast.LENGTH_SHORT).show();
            textView.setText("HELLLLLOOOO");

        }
    });


}

解决方法

您可以尝试在 onCreateView 中进行膨胀操作,或者您唯一的问题是

直接使用 getSystemService 而不使用 getContext 所以先创建 Context 上下文

然后在此上下文中使用 getSystemService 将是另一种选择。

在活动中你可以尝试;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,@Nullable ViewGroup container,@Nullable Bundle savedInstanceState) {
                  inflater =(LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View addedView = inflater.inflate(R.layout.sublayout,container,false);
        button3by3 = addedView.findViewById(R.id.button_3by3);

        button3by3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

              
              //related code comes here

            }
        });

        return addedView;

    }
}

PS:您也可以在片段中尝试这些。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...