将片段中的布尔值传递给活动

时间:2017-01-24 13:39:11

标签: android android-fragments

我是Android开发的新手,我正在尝试将对话框片段中的布尔值传递给活动。

布尔值应该由用户决定(取决于用户点击的按钮)。但是,布尔值在没有单击任何按钮的情况下立即变为false。

我尝试过在互联网上找到的推荐的各种方法,但它们都不适用于我(我想我有一些部分搞砸了......),这些方法包括:

-broadcasting - 实现界面 -intent.putExtra

以下是我提出的代码,有人可以帮我看看吗?任何帮助表示赞赏。

阶段:

package com.example.fuj.valorsafeworldbytrade;

import android.support.v4.app.FragmentManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import static com.example.fuj.valorsafeworldbytrade.LosingDialogFragment.LOSING_FRAGMENT;

public class Stage extends AppCompatActivity {

String stage;
FragmentManager fragmentManager = getSupportFragmentManager();
LosingDialogFragment losingDialogFragment = new LosingDialogFragment();
BasicInfo basicInfo = new BasicInfo();

public void setText(){
    TextView cpuReputation = (TextView) findViewById(R.id.cpu_reputation);
    TextView cpuGold = (TextView) findViewById(R.id.cpu_gold);
    TextView pGoldText = (TextView) findViewById(R.id.player_gold);
    TextView pRepText = (TextView) findViewById(R.id.player_reputation);
    cpuReputation.setText(String.valueOf(basicInfo.cRep));
    cpuGold.setText(String.valueOf(basicInfo.cGold));
    pGoldText.setText(String.valueOf(basicInfo.pGold));
    pRepText.setText(String.valueOf(basicInfo.pRep));
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_stage);
    setText();

}

protected void onStart() {
    super.onStart();
    stage = getIntent().getStringExtra(StageChoosingMenu.STAGE);
}


public void playerChoice(View view) {
    boolean deceiveEnabled;
    switch (view.getId()) {
        case R.id.cooperate_button:
            deceiveEnabled = false;
            break;

        case R.id.deceive_button:
            deceiveEnabled = true;
            break;

        default:
            throw new  RuntimeException("Unknown Button ID");
    }
    switch (stage){
        case "xumo":
            xuMo(deceiveEnabled);
            break;

    }


}

public void xuMo(boolean playerDeceiveEnabled){
    boolean cpuDeceiveEnabled;
    cpuDeceiveEnabled = (Math.random() - basicInfo.faith > 0);

    if (cpuDeceiveEnabled){
        if (playerDeceiveEnabled){
            basicInfo.playerDvsCpuD();
            // faith changes to be amend w/ proper value, need record on the change of status
        }
        if (!playerDeceiveEnabled){
            basicInfo.playerCvsCpuD();
        }
    }

    if (!cpuDeceiveEnabled){
        if (playerDeceiveEnabled){
            basicInfo.playerDvsCpuC();
        }
        if (!playerDeceiveEnabled){
            basicInfo.playerCvsCpuC();
        }
    }
    if(basicInfo.pGold <= 0 || basicInfo.cGold <= 0 || basicInfo.pRep <= 0 || basicInfo.cRep <= 0){
        //to be changed
        setText();
        losingDialogFragment.show(fragmentManager,LOSING_FRAGMENT);
//trying to show a alert dialog fragment
        Log.d("True", String.valueOf(losingDialogFragment.tryAgainEnabled));

        if(losingDialogFragment.tryAgainEnabled){//tryAgainEnabled is always false
//This is the part that I wanted to retrieve data from the user(if they want to try again or not)
            basicInfo.reset();
            losingDialogFragment.dismiss();

        }else{
            losingDialogFragment.dismiss();
        }
    }
    setText();
        // to be changed

}
}

LosingDialogFragment:

package com.example.fuj.valorsafeworldbytrade;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
/**
 * Created by fuj on 20/1/2017.
 */

public class LosingDialogFragment extends DialogFragment{

public static final String LOSING_FRAGMENT = "LOSING";
public boolean tryAgainEnabled;

Intent intent = new Intent();


@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    View rootView = inflater.inflate(R.layout.fragment_lose, container, false);;
//the following code is used to set the boolean value after the user click the button

    final Button tryAgainButton = (Button)rootView.findViewById(R.id.try_again_button);
    tryAgainButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            tryAgainEnabled = true;
        }
    });

    Button giveUpButton =(Button)rootView.findViewById(R.id.give_up_button);
    giveUpButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            tryAgainEnabled = false;
        }
    });
    getDialog().setTitle(LOSING_FRAGMENT);
    return rootView;
}
}

我很抱歉,我不是最好的英语,如果因为我的英语不好而有任何不清楚或不礼貌的事情,请告诉我。对于我所犯的任何错误,我事先道歉。

2 个答案:

答案 0 :(得分:0)

public boolean tryAgainEnabled;活动中声明Stage,您可以使用((Stage)context).tryAgainEnabled更新该变量。

    package com.example.fuj.valorsafeworldbytrade;

import android.support.v4.app.FragmentManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import static com.example.fuj.valorsafeworldbytrade.LosingDialogFragment.LOSING_FRAGMENT;

public class Stage extends AppCompatActivity {

String stage;
FragmentManager fragmentManager = getSupportFragmentManager();
LosingDialogFragment losingDialogFragment = new LosingDialogFragment();
BasicInfo basicInfo = new BasicInfo();
public boolean tryAgainEnabled;
public void setText(){
    TextView cpuReputation = (TextView) findViewById(R.id.cpu_reputation);
    TextView cpuGold = (TextView) findViewById(R.id.cpu_gold);
    TextView pGoldText = (TextView) findViewById(R.id.player_gold);
    TextView pRepText = (TextView) findViewById(R.id.player_reputation);
    cpuReputation.setText(String.valueOf(basicInfo.cRep));
    cpuGold.setText(String.valueOf(basicInfo.cGold));
    pGoldText.setText(String.valueOf(basicInfo.pGold));
    pRepText.setText(String.valueOf(basicInfo.pRep));
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_stage);
    setText();

}

protected void onStart() {
    super.onStart();
    stage = getIntent().getStringExtra(StageChoosingMenu.STAGE);
}


public void playerChoice(View view) {
    boolean deceiveEnabled;
    switch (view.getId()) {
        case R.id.cooperate_button:
            deceiveEnabled = false;
            break;

        case R.id.deceive_button:
            deceiveEnabled = true;
            break;

        default:
            throw new  RuntimeException("Unknown Button ID");
    }
    switch (stage){
        case "xumo":
            xuMo(deceiveEnabled);
            break;

    }


}

public void xuMo(boolean playerDeceiveEnabled){
    boolean cpuDeceiveEnabled;
    cpuDeceiveEnabled = (Math.random() - basicInfo.faith > 0);

    if (cpuDeceiveEnabled){
        if (playerDeceiveEnabled){
            basicInfo.playerDvsCpuD();
            // faith changes to be amend w/ proper value, need record on the change of status
        }
        if (!playerDeceiveEnabled){
            basicInfo.playerCvsCpuD();
        }
    }

    if (!cpuDeceiveEnabled){
        if (playerDeceiveEnabled){
            basicInfo.playerDvsCpuC();
        }
        if (!playerDeceiveEnabled){
            basicInfo.playerCvsCpuC();
        }
    }
    if(basicInfo.pGold <= 0 || basicInfo.cGold <= 0 || basicInfo.pRep <= 0 || basicInfo.cRep <= 0){
        //to be changed
        setText();
        losingDialogFragment.show(fragmentManager,LOSING_FRAGMENT);
        Log.d("True", String.valueOf(losingDialogFragment.tryAgainEnabled));

        if(losingDialogFragment.tryAgainEnabled){//tryAgainEnabled is always false
            basicInfo.reset();
            losingDialogFragment.dismiss();
        }else{
            losingDialogFragment.dismiss();
        }
    }
    setText();
        // to be changed

}
}

并在Fragment中

package com.example.fuj.valorsafeworldbytrade;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
/**
 * Created by fuj on 20/1/2017.
 */

public class LosingDialogFragment extends DialogFragment{

public static final String LOSING_FRAGMENT = "LOSING";
Context context;
Intent intent = new Intent();


@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    View rootView = inflater.inflate(R.layout.fragment_lose, container, false);;

    final Button tryAgainButton = (Button)rootView.findViewById(R.id.try_again_button);
    tryAgainButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ((Stage)context).tryAgainEnabled = true;
        }
    });

    Button giveUpButton =(Button)rootView.findViewById(R.id.give_up_button);
    giveUpButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ((Stage)context).tryAgainEnabled = false;
        }
    });
    getDialog().setTitle(LOSING_FRAGMENT);
    return rootView;
}
}

答案 1 :(得分:0)

这是因为您在显示tryAgainEnabled后正好检查了Dialog的值。 Dialog启动Asynchronously,这意味着它从不同的Thread开始,而您当前的线程并没有等待解雇Dialog,所以代码的这一行{{ 1}}在您显示对话框后完全运行,因为您将值设置为if(losingDialogFragment.tryAgainEnabled){。由于tryAgainEnabled的默认值始终为boolean,因此您每次都会获得false

我建议使用侦听器:

对话框:

false

在您的活动通话对话框中:

public class LosingDialogFragment extends DialogFragment {

    public static final String LOSING_FRAGMENT = "LOSING";
    public boolean tryAgainEnabled;

    Intent intent = new Intent();


    private View.OnClickListener onTryAgainButtonClickLisnter;
    private View.OnClickListener onGiveUpButtonClickLisnter;
    @Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View rootView = inflater.inflate(R.layout.fragment_lose, container, false);;

        final Button tryAgainButton = (Button)rootView.findViewById(R.id.try_again_button);
        tryAgainButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            if(onTryAgainButtonClickLisnter!=null)
                onTryAgainButtonClickLisnter.onClick(v);
            }
        });

        Button giveUpButton =(Button)rootView.findViewById(R.id.give_up_button);
        giveUpButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onGiveUpButtonClickLisnter!=null)
                    onGiveUpButtonClickLisnter.onClick(v);
            }
        });
        getDialog().setTitle(LOSING_FRAGMENT);
        return rootView;
    }

    public void setOnTryAgainButtonClickLisnter(View.OnClickListener onTryAgainButtonClickLisnter) {
        this.onTryAgainButtonClickLisnter = onTryAgainButtonClickLisnter;
    }

    public void setOnGiveUpButtonClickLisnter(View.OnClickListener onGiveUpButtonClickLisnter) {
        this.onGiveUpButtonClickLisnter = onGiveUpButtonClickLisnter;
    }
}