0

プラグイン ビューを使用できるホーム オートメーション アプリを作成しています。別のプロジェクト (apk) でサンプル プラグインとしてクラスを作成できました。

public class MyTestClass_IRDroidUIPlugIn extends Button implements IRDroidInterface{
    Context mContext;
    public MyTestClass_IRDroidUIPlugIn(Context context) {
        super(context);
        mContext = context;
        setText("I was loaded dynamically! (1)");
        setOnClickListener(new View.OnClickListener() {  
            public void onClick(View v) {  
                    // how do I show the dialog from here? 
                Activity.showDialog(1);
            }}  
                );
    }
    public Dialog buildConfigDialog(int ID){
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setMessage("Click the Button...(1)")
           .setCancelable(false)
           .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                    dialog.dismiss();
               }           
           });
        return builder.create();
    }

}

実行時にこのクラスをロードして、そのインスタンスを作成できます。

        try {
        final File filesDir = this.getFilesDir();
        final File tmpDir = getDir("dex", 0);
        final DexClassLoader classloader = new DexClassLoader( filesDir.getAbsolutePath()+"/testloadclass.apk",
                tmpDir.getAbsolutePath(),
                null, this.getClass().getClassLoader());
        final Class<View> classToLoad = 
                (Class<View>) classloader.loadClass("com.strutton.android.testloadclass.MyTestClass_IRDroidUIPlugIn");
        mybutton = (View) classToLoad.getDeclaredConstructor(Context.class).newInstance(this);
        mybutton.setId(2);
        main.addView((View)mybutton);
      } catch (Exception e) {
        e.printStackTrace();
    }

    setContentView(main);
}
protected Dialog onCreateDialog(int id) {
    switch (id) {
        case 1:
            return ((IRDroidInterface) mybutton).buildConfigDialog(id);
    }
    return null;
}

プラグインで設定ダイアログを表示できるようにしたい。Activity オブジェクトをこのクラスに渡して、.showDialog(ID) を使用できるようにする方法はありますか。これは、ダイアログのライフサイクルを適切に管理できるようにするのに理想的です。

前もって感謝します。

4

1 に答える 1

2

多分私は何かが足りないのですが、なぜあなたはこのようなことをすることができないのですか?

public class MyTestClass_IRDroidUIPlugIn extends Button implements IRDroidInterface{
    Activity mContext;
    public MyTestClass_IRDroidUIPlugIn(Activity context) {
        super(context);
        mContext = context;
        ...
        setOnClickListener(new View.OnClickListener() {  
            public void onClick(View v) {  
                mContext.showDialog(1);
            }}  

        );

.....
}

このクラスを作成しているときは、すでにこのクラスに渡さActivityれているので、そのまま使用してください。

于 2012-04-19T17:07:02.563 に答える