在我的片段中,我想显示一个对话框,以在调用另一个函数并移至另一个活动之前插入一些数据,但是该对话框一直被自动关闭,并且在dialog.show()之后的下一行代码是否被调用我是否完成对话。 这是我的对话框的代码:
package com.technion.doggyguide;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.fragment.app.DialogFragment;
import com.technion.doggyguide.R;
import javax.annotation.Nullable;
public class GoogleSignInDialog extends DialogFragment {
private static final String TAG = "GOOGLE DIALOG";
public interface OnInputListener {
void sendInput(String dogname, String dogbreed);
}
public OnInputListener mOnInputListener;
private EditText mDogName, mDogBreed;
private TextView mSet, mCancel;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_google_sign_in, container, false);
mDogBreed = view.findViewById(R.id.input_breed);
mDogName = view.findViewById(R.id.input_name);
mSet = view.findViewById(R.id.action_ok);
mCancel = view.findViewById(R.id.action_cancel);
mCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getDialog().dismiss();
}
});
mSet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String dogname = mDogName.getText().toString();
String dogbreed = mDogBreed.getText().toString();
if (dogname.isEmpty() || dogbreed.isEmpty()) {
Toast.makeText(getContext(),
"Please enter the name and breed of your dog" ,Toast.LENGTH_LONG).show();
return;
}
mOnInputListener.sendInput(dogname, dogbreed);
getDialog().dismiss();
}
});
return view;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
mOnInputListener = (OnInputListener) getTargetFragment();
} catch (ClassCastException e) {
Log.d(TAG, e.getMessage());
}
}
}
这是我的片段的代码:
private void signIWithGoogle(final GoogleSignInAccount account) {
Log.d("TAG", "firebaseAuthWithGoogle:" + account.getId());
final AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null);
final CollectionReference dogowners = db.collection("dog owners");
dogowners.whereEqualTo("email", account.getEmail())
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
List<DocumentSnapshot> docs = task.getResult().getDocuments();
if (docs.isEmpty()) {
GoogleSignInDialog dialog = new GoogleSignInDialog();
dialog.setTargetFragment(DogOwnerConnectionFragment.this, 1);
dialog.show(getFragmentManager(), "MyCustomDialog");
firstGoogleSignIn(credential, account);
} else if (docs.size() == 1) {
GoogleSignIn(credential, account);
}
}
});
}
真正的问题是如何在完成对话框之前停止运行代码?
答案 0 :(得分:0)
首先:让您的Fragment实现OnInputListener并覆盖功能
第二个:创建对话框后的setListener
GoogleSignInDialog dialog = new GoogleSignInDialog();
dialog.setListener(this);
.
.
GoogleSignInDialog
void setListener(OnInputListener listener){
mOnInputListener = listener;
}
然后,您可以使用此接口回调片段,然后在关闭对话框之前执行某些操作
mCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.doSomething()//do anything you want
getDialog().dismiss();
}
});
希望这会有所帮助。