5

このダイアログでは、他のアプリをインストールするかどうかを尋ねられます。そのため、ボタンをクリックしないと、前の画面に戻る必要があります。

    downloadDialog.setNegativeButton(stringButtonNo,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialogInterface, int i) {
                         finish();
                }
            });

これによりエラーが発生します:

メソッドfinish()は、タイプnew DialogInterface.OnClickListener(){}に対して未定義です。

どうすれば私が望んでいたことを達成できますか?

package com.Android.barcode;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class BarcodeActivity extends Activity {
    public static String upc;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        IntentIntegrator.initiateScan(this);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
        case IntentIntegrator.REQUEST_CODE: {
            if (resultCode != RESULT_CANCELED) {
                IntentResult scanResult = IntentIntegrator.parseActivityResult(
                        requestCode, resultCode, data);
                if (scanResult != null) {
                    upc = scanResult.getContents();

                    Intent intent = new Intent(BarcodeActivity.this, BarcodeResult.class);
                    startActivity(intent);
                    // put whatever you want to do with the code here
/*                  TextView tv = new TextView(this);
                    tv.setText(upc);
                    setContentView(tv);*/
                }
            }
            break;
        }
        }
    }
}
4

4 に答える 4

9

そのアクティビティからそのダイアログを作成したくないので:2つのオプションがあります

1)ユーザーに行ってもらいたいアクティビティにインテントをコールバックします。

Intent intent = new Intent(getBaseContext(), theActivity.class); 
getApplication().startActivity(intent) ;

またはその他

2)ダイアログで構成されるそのクラスのコンストラクターを作成します。

public class ABC {
    Context iContext=null;
   public ABC(Context con){
    iContext=con;
   }
 ....

}

アクティビティのコンテキストを使用してクラスを呼び出します。のようにABC(Cont)。そして、そのクラス内で使用((Activity)iContext).finish()して、必要に応じてそのアクティビティを終了します。

于 2012-05-19T05:06:14.110 に答える
6

uよりもContextが割り当てられているコンストラクターを持つクラスがこのように使用できる場合

   AlertDialog.Builder adb=new AlertDialog.Builder(context);
                adb.setTitle("Are You Sure Want To Delete?");
                adb.setPositiveButton("OK", new AlertDialog.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {

                    }});
                adb.setNegativeButton("CANCEL", new AlertDialog.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        ((Activity) context).finish();
                    }});
                adb.show();
于 2012-05-19T05:08:38.983 に答える
2

メソッドfinish()は、タイプnew DialogInterface.OnClickListener(){}に対して未定義です。

DialogInterface.OnClickListenerそのような方法がないため、このエラーが発生する可能性があります。アクティビティを終了したい場合は、使用する必要があります

ActivityName.this.finish();
于 2012-05-19T04:56:55.690 に答える
1

最善の解決策は、あらゆるタイプのダイアログにダイアログフラグメントを使用することです。これにより、アクティビティのダイアログが開きます。そしてリスナーでこのダイアログを削除します。次のリンクをご覧ください。

http://developer.android.com/reference/android/app/DialogFragment.html

Androidの方からもお勧めです。

于 2012-05-19T05:04:38.910 に答える