如何实现在非活性类机器人像“完成”?(how to achieve something like “

2019-09-17 08:17发布

此对话框询问你是否要安装一些其他的应用程序...所以onclicked没有按钮时,必须返回到前一个屏幕

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

这给出了错误:

该方法结束()是未定义的类型新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;
        }
        }
    }
}

Answer 1:

既然你不想创建该活动是对话:你有两个选择

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()该类内,你想完成该活动。



Answer 2:

如果有构造你的类,它有语境中它分配比u可以使用这种方式

   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();


Answer 3:

该方法结束()是未定义的类型新DialogInterface.OnClickListener(){}

这很可能给这个错误,因为DialogInterface.OnClickListener没有任何这样的方法。 如果你想完成你的活动,你必须使用

ActivityName.this.finish();


Answer 4:

最好的解决办法是使用对话片段的任何类型的对话框中,将刚打开你的活动的对话框。 而就听众删除此对话框。 请有以下链接看看:

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

它是从Android的人推荐为好。



文章来源: how to achieve something like “finish” in a non-activity class in android?