如何在 Android 中访问另一个模块中的一个模块的视图?
How to access view of one module in another module in Android?
我的要求是与另一个模块(比如库模块)共享 xml 一个模块(比如应用程序)的视图。我该怎么做?
我试过这种方法,但是onClickListener按钮不起作用。我哪里错了?
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
intent.putExtra(Config.LAYOUT_ID, R.layout.login_view);
startActivityForResult(intent, Config.LOGIN_REQUEST);
在login_view.xml
<Button
android:id="@+id/login_button"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="4dp"
android:layout_weight="1"
android:tag="login_button"/>
在不同模块的 LoginActivity 的 onCreate 方法中
int layoutId = getIntent().getIntExtra(Config.LAYOUT_ID, 0);
if (layoutId != 0) {
setContentView(layoutId);
View rootView = LayoutInflater.from(getApplicationContext()).inflate(layoutId, null);
loginButton = (Button)rootView.findViewWithTag("login_button");
if (loginButton != null) {
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(LoginActivity.this, "Logging in...", Toast.LENGTH_SHORT).show();
}
});
}
一切顺利,连控件都进入了if (loginButton != null)
状态。但是当按钮被点击时,没有任何反应。我哪里错了?或者这种方法行得通吗?如果不行,有什么办法吗?
P.S:我还尝试通过 Intent 传递按钮 ID 并通过该 ID 查找视图。完全相同的结果。
试试这个很管用
替换
setContentView(layoutId);
和
View rootView = LayoutInflater.from(getApplicationContext()).inflate(layoutId, null);
setContentView(rootView );
更新这个。
int layoutId = getIntent().getIntExtra(Config.LAYOUT_ID, 0);
if (layoutId != 0) {
View rootView = LayoutInflater.from(getApplicationContext()).inflate(layoutId, null);
setContentView(rootView );
loginButton = (Button)rootView.findViewWithTag("login_button");
if (loginButton != null) {
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(LoginActivity.this, "Logging in...", Toast.LENGTH_SHORT).show();
}
});
}
我的要求是与另一个模块(比如库模块)共享 xml 一个模块(比如应用程序)的视图。我该怎么做?
我试过这种方法,但是onClickListener按钮不起作用。我哪里错了?
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
intent.putExtra(Config.LAYOUT_ID, R.layout.login_view);
startActivityForResult(intent, Config.LOGIN_REQUEST);
在login_view.xml
<Button
android:id="@+id/login_button"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="4dp"
android:layout_weight="1"
android:tag="login_button"/>
在不同模块的 LoginActivity 的 onCreate 方法中
int layoutId = getIntent().getIntExtra(Config.LAYOUT_ID, 0);
if (layoutId != 0) {
setContentView(layoutId);
View rootView = LayoutInflater.from(getApplicationContext()).inflate(layoutId, null);
loginButton = (Button)rootView.findViewWithTag("login_button");
if (loginButton != null) {
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(LoginActivity.this, "Logging in...", Toast.LENGTH_SHORT).show();
}
});
}
一切顺利,连控件都进入了if (loginButton != null)
状态。但是当按钮被点击时,没有任何反应。我哪里错了?或者这种方法行得通吗?如果不行,有什么办法吗?
P.S:我还尝试通过 Intent 传递按钮 ID 并通过该 ID 查找视图。完全相同的结果。
试试这个很管用
替换
setContentView(layoutId);
和
View rootView = LayoutInflater.from(getApplicationContext()).inflate(layoutId, null);
setContentView(rootView );
更新这个。
int layoutId = getIntent().getIntExtra(Config.LAYOUT_ID, 0);
if (layoutId != 0) {
View rootView = LayoutInflater.from(getApplicationContext()).inflate(layoutId, null);
setContentView(rootView );
loginButton = (Button)rootView.findViewWithTag("login_button");
if (loginButton != null) {
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(LoginActivity.this, "Logging in...", Toast.LENGTH_SHORT).show();
}
});
}