如何绑定Click Listener to Button从另一个Activity添加到Inflater在Android中

时间:2018-06-13 11:45:29

标签: android events

我一直在尝试将点击监听器添加到我在活动中创建的按钮,但布局已通过Inflater添加到另一个活动中,但按钮未接收到下面的事件是我的代码

XML布局

 <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"/>

主要活动

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

}
} 

主活动的edit_profile按钮拒绝接受点击监听器。

BaseActivity中扩展MainActivity的本质是因为BaseActivity包含大多数活动将继承的基本功能

使用MainActivity的{​​{1}}方法从BaseActivity加载了initAddLayout的布局

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

您可以按如下方式实现:

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

    }
}

<强> MainActivity

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);
   }
   ...
}