如何使用android中的对话框打开新活动

时间:2016-03-21 09:54:46

标签: android

我无法在班级中使用startActivity()函数,现在我被卡住了。但我必须从这堂课开一个新的活动。请帮帮我......

这是我的课程,我想通过使用此类中的onClick事件打开一个新活动,该事件定义如下

package com.fva_001.flashvsarrow.com.fva_001;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.os.*;
import android.view.View;
import android.view.Window;
import android.widget.Button;


public class MapFoundDialog extends Dialog implements android.view.View.OnClickListener {

public Activity c;
public Dialog d;
public Button yes;

public MapFoundDialog(Activity a) {
    super(a);
    // TODO Auto-generated constructor stub
    this.c = a;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
    setContentView(R.layout.dialog_map_found);
    yes = (Button) findViewById(R.id.btn_hide_dialog_map_found);
    yes.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    new ButtonClick(getContext(), v);
    switch (v.getId()) {
        case R.id.btn_map_open:
            // i want to open a new activity from here....

            break;
        case R.id.btn_hide_dialog_map_found:
            dismiss();
            break;
        default:
            break;
    }
    dismiss();
}
}

1 个答案:

答案 0 :(得分:0)

首先,我不建议将Activity传递给对话框,因为它可能会导致Activity Leak,这是严重的性能问题!

为什么你将活动传递给对话框?您可以传递ApplicationContext。

使用此代码:

public class MapFoundDialog extends Dialog implements android.view.View.OnClickListener {

public Context c;
public Dialog d;
public Button yes;

public MapFoundDialog(Context c) {
    super(c);
    // TODO Auto-generated constructor stub
    this.c = c.getApplicationContext();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
    setContentView(R.layout.dialog_map_found);
    yes = (Button) findViewById(R.id.btn_hide_dialog_map_found);
    yes.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    new ButtonClick(c, v);
    switch (v.getId()) {
        case R.id.btn_map_open:
            // i want to open a new activity from here....
             Intent intent = new Intent(c, NewActivity.class);
             c.startActivity(intent);
             break;

        case R.id.btn_hide_dialog_map_found:
            dismiss();
            break;
        default:
            break;
    }
    dismiss();
}
}