getActivity使用getActivity

2023-09-07 02:19:24 作者:高潮来不停

中首先对不起,如果这个职位似乎是一个重复的,但我很新到Android编程和张贴了这个问题,只有当我还是没能得到满意的答复使用getActivity的。

First of all sorry if this post may seem a duplicate but I am very new to Android programming and posting this question only when I am still not able to get a satisfactory answer for use of getActivity.

理论上我理解从这里的几个职位使用getActivity(),但我很困惑,它是如何在我的code工作。

Theoretically I understand the use of getActivity() from the several posts here but I am confused how it is working in my code.

我从我创建一个复选框的对话框的onclick类MainActivity。我有另一个类TaxDialog其中实现的对话框。 Yes(是)的点击/否按钮我打电话的方法定义MainActivity类别。 下面是codeS:

I have a class MainActivity from which I am creating a dialog onclick of a checkbox. I have another class TaxDialog where the dialog is implemented. On click of Yes/No buttons I am calling methods define in MainActivity class. Below are the codes:

MainActivty

    import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;



public class MainActivity extends ActionBarActivity implements View.OnClickListener, View.OnFocusChangeListener {

// onCheck of checkbox showNoticeDialog is called

    public void showNoticeDialog() {
        // Create an instance of the dialog fragment and show it
        Log.i("MyActivity", "Inside showNoticeDialog");
        DialogFragment dialog = new TaxDialog();
        Bundle args = new Bundle();
        args.putString("title", "Test");
        args.putString("message", "Test Message");
        dialog.setArguments(args);

        dialog.show(getSupportFragmentManager(), "dialog");


    }
   public void doPositiveClick(){
       Log.i("MyActivity", "Inside +ve");
   }
    public void doNegativeClick(){
        Log.i("MyActivity", "Inside -ve");
    }

}

TaxDialog

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.Log;

public class TaxDialog extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Bundle args = getArguments();

        String title = args.getString("title");

        String message = args.getString("message");
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(title);

        builder.setMessage(message);
        builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, null);
                Log.i("MyActivity", "Expected fault area.");
                ((MainActivity) getActivity()).doPositiveClick();
            }
        });
        builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ((MainActivity) getActivity()).doNegativeClick();
            }
        });
        builder.create();

        return builder.create();
    }

}

在这里,我想了解以下行是如何工作的。

Here I want to understand how below line works

((MainActivity) getActivity()).doPositiveClick();

,也请让我知道做同样的事情(像MainActivity.this.getActivity()或别的东西)的其他方式。

and also please make me aware of other ways of doing the same thing (something like MainActivity.this.getActivity() or something else).

非常感谢。

修改

谢谢大家。大概是我陷害不正确的问题。我唯一​​的疑问是getActivity()如何返回活动的参考。现在我明白了。

Thanks everyone. Probably I framed the question incorrectly. My only doubt was how getActivity() returns the Activity reference. Now I understand.

推荐答案

让分起来:

getActivity()可以在一个片段被用于获取父活动片段(基本上是显示的片段活动)。

getActivity() can be used in a Fragment for getting the parent Activity of the Fragment (basically the Activity that displayed the Fragment).

((MainActivity)getActivity())此外蒙上) getActivity的结果(来MainActivity 。通常情况下,你只知道 getActivity()返回类型的对象活动,但使用这种的投的,你告诉你是肯定的对象是活动的一个具体子类的编译器,即 MainActivity 。你需要做的,在你的情况,因为 doPositiveClick(),而不是仅在 MainActivity 实施活动,所以你要确保编译对象是 MainActivity ,它将给方法回应 doPositiveClick()

((MainActivity) getActivity()) additionally casts the result of getActivity() to MainActivity. Usually, you just know that getActivity() returns an object of type Activity, but using this cast, you tell the compiler that you are sure the object is a specific subclass of Activity, namely MainActivity. You need to do that in your case, because doPositiveClick() is only implemented in MainActivity, and not in Activity, so you have to assure the compiler the object is a MainActivity, and it will respond to the method doPositiveClick().

doPositiveClick()简单地调用由 getActivity()。

doPositiveClick() simply calls that method on the MainActivity object that is returned by getActivity().

 
精彩推荐
图片推荐