如何实现像"完成"在Android的非活动类? [英] how to achieve something like "finish" in a non-activity class in android?

查看:214
本文介绍了如何实现像"完成"在Android的非活动类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  downloadDialog.setNegativeButton(stringButtonNo,
            新DialogInterface.OnClickListener(){
                公共无效的onClick(DialogInterface dialogInterface,int i)以{
                         完();
                }
            });

这给出了错误:

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

我如何能实现我想要的?

 包com.Android.bar code;进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.widget.TextView;公共类酒吧codeActivity延伸活动{
    公共静态字符串UPC;    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        IntentIntegrator.initiateScan(本);
    }    保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
        开关(要求code){
        案例IntentIntegrator.REQUEST_ code:{
            如果(结果code!= RESULT_CANCELED){
                IntentResult scanResult = IntentIntegrator.parseActivityResult(
                        要求code,结果code,数据);
                如果(scanResult!= NULL){
                    UPC = scanResult.getContents();                    意向意图=新意图(酒吧codeActivity.this,酒吧codeResult.class);
                    startActivity(意向);
                    //把任何你想在这里与code做
/ * TextView的电视=新的TextView(本);
                    tv.setText(UPC);
                    的setContentView(电视); * /
                }
            }
            打破;
        }
        }
    }
}


解决方案

既然你不想创建该活动的对话框:你有两种选择

1)调用的意图回到你希望用户去活动。

 意向意图=新意图(getBaseContext(),theActivity.class);
。getApplication()startActivity(意向);

或Else

2)该类由对话框创建一个构造函数。

 公共类ABC {
    上下文iContext = NULL;
   公共ABC(上下文CON){
    iContext = CON;
   }
 ....}

调用类的活动的情况下。像 ABC(续)。而再使用((活动)iContext).finish()该类内完成如你所愿的活动。

This dialog asks whether you want to install some other app...so when onclicked no button it must go back to the previous screen

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

this gives the error:

The method finish() is undefined for the type new DialogInterface.OnClickListener(){}

how can i achieve what i wanted???

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;
        }
        }
    }
}

解决方案

Since you don't want to create that dialog from that activity : You have two options

1) Call an Intent back to the activity you want the user to go to.

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

or Else

2) Create a constructor for that class consisting of the dialog.

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

}

Call the class with the Context of the activity. Like ABC(Cont) .And then use ((Activity)iContext).finish() within that class to finish that activity as you wish.

这篇关于如何实现像"完成"在Android的非活动类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆