Android Zugriff auf view von MainActivity

RezaScript

Bekanntes Mitglied
Hallo,

In MainActivity habe ich einen Knopf. Wenn ich darauf drucke, öffnet sich ein Dialog. Für den Dialog habe ich folgende Klasse erstellt:

Java:
public class Popup extends DialogFragment {
    private final int _layout;

    @SuppressLint("ValidFragment")
    public Popup(int layout) {
        _layout = layout;
    }

    @SuppressLint({"ClickableViewAccessibility", "ResourceType"})
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Nullable
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        final View view =  inflater.inflate(_layout, container, false);
        if (_layout == R.layout.fragment_dialog) {
        ...

In meinem Dialog habe ich auch einen Knopf und wenn ich da drauf klicke, möchte ich den Background vom MainActivity-Knopf ändern.

Wie kann ich also von meiner Klasse aus auf view von MainActivity zugreifen?
 
Du brauchst den Context der Activity.
Entweder gibst du ihn der Methode aus deiner anderen Klasse mit .

Oder du übergibst ihn beim erzeugen der Instanz deiner Klasse an den Kostrucktor.
Machst also einen Kostrucktor der den Context erwartet und den speicherst du in einer Instanzvariablen in der Klasse.
 
Und was soll ich dann mit dem Context anfangen?

Ich hab's mal so probiert:
Java:
private Context mContext;

@SuppressLint("ValidFragment")
public Popup(int layout) {
    _layout = layout;
}

public Popup(Context context, int layout) {
    mContext = context;
    _layout = layout;
}

Und so versuche ich den Context einzusetzen:
Java:
mContext.findViewById(R.id.dialog).setBackground(ContextCompat.getDrawable(getContext(), R.drawable.button_yellow));
Bekomme aber die Fehlermeldung: Cannot resolve method 'findViewById' in 'Context'
 
zeige alles nicht nur diese Stücken .
die kommplette Klasse bitte

eigentlich
Java:
mContext.getApplicationContext().findViewById(R.id.dialog) .....
 
Zuletzt bearbeitet:
Java:
@SuppressLint("ValidFragment")
public class Popup extends DialogFragment {
    private final int _layout;
    private final Dialog mDialog = new Dialog();
    public boolean dialogIsActive = false;
    private Context mContext;

    @SuppressLint("ValidFragment")
    public Popup(int layout) {
        _layout = layout;
    }

    public Popup(Context context, int layout) {
        mContext = context;
        _layout = layout;
    }

    @SuppressLint({"ClickableViewAccessibility", "ResourceType"})
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Nullable
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        final View view =  inflater.inflate(_layout, container, false);

        // Display fragment_dialog
        if (_layout == R.layout.fragment_dialog) {
            // Toggle the listener
            Button cta = view.findViewById(R.id.dialogCta);
            cta.setOnClickListener(v -> {
                if (!dialogIsActive) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        if (mDialog.hasRecordPermission(getContext())) {
                            mDialog.startService(getContext());
                            cta.setBackground(ContextCompat.getDrawable(requireContext(), R.drawable.button_red));
                            cta.setText(R.string.dialog_do_not_listen);
                            cta.setTextSize(17);
                            mContext.findViewById(R.id.dialog).setBackground(ContextCompat.getDrawable(getContext(), R.drawable.button_red));
                            dialogIsActive = true;
                        }
                    }
                }
                else {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        mDialog.stopTheService(getContext());
                        cta.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.button_yellow));
                        cta.setText(R.string.dialog_listen);
                        mContext.findViewById(R.id.dialog).setBackground(ContextCompat.getDrawable(getContext(), R.drawable.button_yellow));
                        dialogIsActive = false;
                    }
                }
            });
        }
        return view;
    }
}
 
Die "findViewById" Methode gehört nicht zur Context-Klasse, sondern zur View-Klasse, wenn mich nicht alles täuscht. Eigentlich dürfte die IDE dir die gar nicht vorschlagen.

Probier es mal hiermit:
Java:
requireActivity().findViewById(R.id.dialog)....
 
Java:
public class test {

     Activity mActivity;
     Context  mContext;

    public test(Activity activity,Context context) {
        this.mActivity = activity;
        this.mContext = context;
    }

    public void mtest(){
        mActivity.findViewById(R.id.dialog));
        ((Activity)mContext).findViewById(R.id.dialog));
    }
}

etweder du die Activity oder den Context.
 

Zurück
Oben