android - 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.
解决方案
您可以按如下方式实现:
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);
}
...
}
推荐阅读
- neo4j - 哪些数据问题会导致“AmbiguousBaseClassException:在映射到 [...] 的类型层次结构中找到多个类”?
- kubernetes - 如何在 kiali-ui 中“纱线启动”时设置 kiali-ui/package.json 'proxy' 属性?
- mysql - 在触发器中获取动态表名
- php - 如何在php站点的链接上使用api发布数据
- c# - Unity:android.os.DeadObjectException,应用程序在启动时崩溃
- matlab - 符号压缩运算和求和不能得到正确答案
- java - 如何从 Java 中的 IPv6 子网获取所有 IP?
- html - 使用 ng-repeat 解析时,如何仅在第一项上打印内容?
- facebook-graph-api - 获取Access Token Server端Instagram Graph Api V3
- ruby - 当变量可能为假时,如何检查 nil 并在同一行初始化?