首页 > 解决方案 > How to Bind Click Listener to Button added from another Activity through Inflater In Android

问题描述

I have been trying to add click listener to a button which I created in an activity but the layout is been added in another activity through Inflater, but the button is not receiving the event below is my code

XML Layout

 <Button
     android:layout_width="wrap_content"
                android:layout_height="25dp"
                android:layout_alignParentRight="true"
                android:layout_marginEnd="8dp"
                android:layout_marginTop="15dp"
                android:background="@drawable/rounded_transparent_botton"
                android:textAllCaps="false"
                style="@style/transparentButton"
                android:text="Edit Profile"
                android:id="@+id/edit_profile"/>

Main Activity

   public class MainActivity extends BaseActivity implements View.OnClickListener{      

    Button edit_profile;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    initAddLayout(R.layout.activity_profile);
    setActivityTitle(R.string.profile_activity_title);


    loadData();

    FontChangeCrawler fontChanger = new FontChangeCrawler(getAssets(), "fonts/Merriweather-Bold.ttf");
    fontChanger.replaceFonts((ViewGroup) this.findViewById(android.R.id.content));

    this.edit_profile = findViewById(R.id.edit_profile);
    this.edit_profile.setOnClickListener(this);
  }

  @Override
  public void onClick(View view) {
    if (view.getId()==R.id.edit_profile){
        Log.i("TAG", "PROFILE CLICKED");
    }
  }
}

BaseActivity.class

public class BaseActivity extends AppCompatActivity {   

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

  protected void initAddLayout(int layout) {
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(layout, null, false);
        ((FrameLayout) findViewById(R.id.main_content_below)).addView(view);

}
} 

the edit_profile button from main activity refuses to accept the click listener.

The essence of extending BaseActivity in the MainActivity is because BaseActivity contains some base functionality that most activities will inherit

The layout for MainActivity is been loaded from BaseActivity using the initAddLayout method of BaseActivity

Any assistance will be appreciated.

标签: androidevents

解决方案


您可以按如下方式实现:

public class BaseActivity extends AppCompatActivity {   
    protected View profileActivityView;
    ...
    protected void initAddLayout(int layout) {
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        final FrameLayout frameLayout = (FrameLayout)findViewById(R.id.main_content_below);
         profileActivityView = inflater.inflate(layout, frameLayout , false);
        frameLayout.addView(profileActivityView);

    }
}

主要活动

public class MainActivity extends BaseActivity implements View.OnClickListener{      

    Button edit_profile;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initAddLayout(R.layout.activity_profile);
        setActivityTitle(R.string.profile_activity_title);

        ...

        this.edit_profile = profileActivityView.findViewById(R.id.edit_profile);
        this.edit_profile.setOnClickListener(this);
   }
   ...
}

推荐阅读