【问题标题】:Call a method from an activity in custom dialog activity从自定义对话框活动中的活动调用方法
【发布时间】:2013-11-20 05:25:18
【问题描述】:

我正在开发一个 android 应用程序,我在其中使用一个活动作为自定义对话框。我将自定义对话框活动命名为 Dialog_activity,并将我的游戏活动命名为 Activity1。在Dialog_activity 中,有两个按钮,即是和否。该对话框询问用户他/她是否想开始一个新游戏。那么,如何在 yes 按钮的 OnClick 方法中调用 Activity1 中的 Dialog_activity 中的方法。这是一个井字游戏应用程序。 代码如下:

活动1

public class Dialog_activity extends Activity {
Button yesbutton,nobutton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_1);
Button btn1 = (Button) findViewById(R.id.button1);
Button btn2 = (Button) findViewById(R.id.button2);
Button btn3 = (Button) findViewById(R.id.button3);
Button btn4 = (Button) findViewById(R.id.button4);
Button btn5 = (Button) findViewById(R.id.button5);
Button btn6 = (Button) findViewById(R.id.button6);
Button btn7 = (Button) findViewById(R.id.button7);
Button btn8 = (Button) findViewById(R.id.button8);
Button btn9 = (Button) findViewById(R.id.button9);
}

public void resetButtons()
{
btn1.setText("");
btn2.setText("");
btn3.setText("");
btn4.setText("");
btn5.setText("");
btn6.setText("");
btn7.setText("");
btn8.setText("");
btn9.setText("");
}
}

对话活动

public class Dialog_activity extends Activity {
Button yesbutton,nobutton;
@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_activity);
    Intent startdialog = getIntent();
    yesbutton = (Button) findViewById(R.id.button);
    nobutton = (Button) findViewById(R.id.button2);
    nobutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });
    yesbutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
            //call the resetButtons() method
        }
    });

谢谢!

【问题讨论】:

  • 能否在需要帮助的地方发布代码片段?
  • @RDC 是的,我会粘贴代码。
  • 发布您的代码。您应该创建一个自定义对话框,而不是创建一个 dialog_activity !然后就可以轻松调用所需的方法了
  • @RachitaNanda 我已经发布了代码。
  • 创建对话框或片段对话框而不是活动。这将轻松解决您的问题。请发布代码以获得更多帮助。

标签: android dialog android-activity


【解决方案1】:

这里有几个选项:

  1. 您可以将 reset button() 设为静态,但为此您还必须将按钮设为静态。不建议使用此方法。

  2. 使用此How to create a Custom Dialog box in android? 创建自定义对话框并仅在您的 Activity1 中扩展此自定义对话框。您可以将 CustomDialogClass 作为内部类,因此它将能够访问所有 Activity1 方法。

希望对你有帮助。

【讨论】:

  • 那么,从另一个活动中调用一个活动中的方法是不可能的?
  • 您可以按照说明使用第 1 点。但是不建议创建静态变量,除非其至关重要,因为 stackoverflow.com/questions/8563843/static-variable-loses-value
  • 非常感谢您的帮助!
  • 我建议不要在 android 中使用静态引用维护任何 View,因为 Context 与每个 View 相关联,这可能导致上下文泄漏
【解决方案2】:

在Activity1中创建Dialog_activity的实例,使用实例调用你想要的方法。

Class Activity1 extends Activity
{

   Dialog_activity dialog_activity;

    btn.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View arg0)
            {
                dialog_activity.the_method_you_want;

            }
        });
}

【讨论】:

  • @kumar 我的应用程序强制在我使用它时关闭。
  • 当我使用这个时,我的应用程序强制关闭。
【解决方案3】:

我可以建议两种方法,

1) 使用 startActivityForResult() 从 Game Activity 启动 Dialog Activity 并覆盖 GameActivity 中的 onActivityResult()

2) 注册 GameActivity 以使用 registerReceiver() 监听自定义 Intent 动作,并使用 sendBroadCast() 在 DialogActivity 中广播该动作

【讨论】:

    【解决方案4】:

    1.导入活动_1 2.在类下面写类变量Dialog_activity activity1 3.使用这个activity1引用activity1.resetButtons()调用reset方法;在 yesbutton onclocklistener 里面

    1.导入活动_1

    public class Dialog_activity extends Activity {
    Button yesbutton,nobutton;
    

    2。在类下面写类变量Dialog_activity activity1

    Dialog_activity activity1;
    
    @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_activity);
        Intent startdialog = getIntent();
        yesbutton = (Button) findViewById(R.id.button);
        nobutton = (Button) findViewById(R.id.button2);
        nobutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        yesbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {`enter code here`
                finish();
                **//call the resetButtons() method using reference activity1 
                activity1.resetButtons();
            }
        });
    

    【讨论】:

    • 谢谢,但是当我实现这个时,我的应用程序强制关闭了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-03
    • 1970-01-01
    相关资源
    最近更新 更多