简体   繁体   English

如何更改 android 中的按钮 clicklistener 外部活动?

[英]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:我有两个想法可以解决您的问题:

  1. Using Application/Activity method registerActivityLifecycleCallbacks ( link for activity ).使用 Application/Activity 方法registerActivityLifecycleCallbacks活动链接)。 This callback has method onActivityCreated which provides instance of created activity. 此回调具有onActivityCreated方法,该方法提供已创建活动的实例。 In this method you can get Activity's name by calling to 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 链接。

  1. Another solution - you can try to create "stub" activity with name com.example.myapplication.ui.login.LoginActivity then extend it like: public class MyLoginActivity extends LoginActivity .另一种解决方案-您可以尝试创建名为 com.example.myapplication.ui.login.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).然后你可以重写 MyLoginActivity 的 onCreate 方法并做你想做的事(我的意思是为按钮 onClick 监听器添加你的代码)。 But you don't need to build in this "stub" LoginActivity to application (apk), you should add it as compile only dependency.但是您不需要将此“存根”LoginActivity 构建到应用程序(apk)中,您应该将其添加为仅编译依赖项。 I don know how do it with a class but I know other solution: you can put LoginActivity to separate module or library(aar/jar) and add the library(module) as compileOnly dependency to your gradle file ( like here )我不知道如何使用 class 但我知道其他解决方案:您可以将 LoginActivity 放在单独的模块或库(aar/jar)中,并将库(模块)作为compileOnly 依赖项添加到您的 gradle 文件如这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM