[英]How change button clicklistener outside activity in android?
I have an apk that contains classes.dex.我有一个包含 classes.dex 的 apk。 I dont't have apk source code.
我没有apk源代码。 I have loaded an activity from dex file(LoginActivity) and implemented onClicklistener for button1 in LoginActivity.
我已经从 dex 文件(LoginActivity)加载了一个活动,并在 LoginActivity 中为 button1 实现了 onClicklistener。 I succeeded to load dex file and started activity from dex file via this code:
我成功加载 dex 文件并通过以下代码从 dex 文件开始活动:
DexClassLoader dex = new DexClassLoader(dexInternalStoragePath.getAbsolutePath(), mAppContext.getCacheDir().getPath(), null, loader);
toasterClass = dex.loadClass("com.example.myapplication.ui.login.LoginActivity");
Intent intent = new Intent();
intent.setClass(getApplicationContext(),toasterClass);
setContentView(View.inflate(getApplicationContext(), R.layout.activity_login, null));
Button mAddTaskButton = (Button) findViewById(R.id.button2);
mAddTaskButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog aldlg = new AlertDialog.Builder(gLoginActivity.this).create();
aldlg.setTitle("Login");
aldlg.setMessage("You pressed Login Button");
aldlg.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
aldlg.show();
}
});
startActivity(intent);
but when I clicked button2, it didn't show alertdialog.但是当我点击button2时,它没有显示alertdialog。 I think android is preventing to change onClicklistener outside from activity.
我认为 android 正在阻止从活动之外更改 onClicklistener。 Is it correct?
这是对的吗? Any help how to implement this?
任何帮助如何实现这一点?
When you use code:当您使用代码时:
Button mAddTaskButton = (Button) findViewById(R.id.button2)
it means that you try to find view in your current Activity instance (same thing as this.findViewById
)- not from com.example.myapplication.ui.login.LoginActivity
这意味着您尝试在当前 Activity 实例中查找视图(与
this.findViewById
相同)-不是来自com.example.myapplication.ui.login.LoginActivity
I have two ideas to solve your problem:我有两个想法可以解决您的问题:
activity.getComponentName().getClassName()
(link to getComponentName method and to ComponentName class description ).activity.getComponentName().getClassName()
来获取 Activity 的名称(链接到getComponentName方法和ComponentName class 描述)。 If activity provided by onActivityCreated
method is com.example.myapplication.ui.login.LoginActivity
then you can add OnClickListene to button like in your code snippet(just use reference to activity to get Button):onActivityCreated
方法提供的活动是com.example.myapplication.ui.login.LoginActivity
那么您可以像在代码片段中一样将 OnClickListene 添加到按钮(只需使用对活动的引用来获取按钮):Button mAddTaskButton = (Button) activity.findViewById(R.id.button2);
mAddTaskButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
...
}
});
Be careful with ActivityLifecycleCallback because it attaches to application life-cycle (not activity) and don't capture Activity's content from callback to avoid memory links.小心 ActivityLifecycleCallback 因为它附加到应用程序生命周期(不是活动)并且不要从回调中捕获活动的内容以避免 memory 链接。
public class MyLoginActivity extends LoginActivity
.public class MyLoginActivity extends LoginActivity
。 Then you can override MyLoginActivity's onCreate method and do what you want (I mean add your code for button onClick listener).
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.