gpt4 book ai didi

java - 警报对话框 not_showing

转载 作者:行者123 更新时间:2023-11-29 09:51:46 24 4
gpt4 key购买 nike

我是安卓开发的新手。试图在我的项目中实现 android 对话框,但它没有显示。logcat 中没有错误。

这是它的 XML 代码:

<android.support.design.widget.FloatingActionButton
android:id="@+id/add_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:clickable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.927"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.935"
app:srcCompat="@android:drawable/ic_input_add" />

这是我实现了 alertdialog 的部分:

FloatingActionButton add_button = findViewById(R.id.add_item);
add_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Create item list");
final EditText enter_item_list=new EditText(MainActivity.this);
enter_item_list.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
enter_item_list.setHint("Type a name");
enter_item_list.setHintTextColor(Color.RED);
builder.setView(enter_item_list);

builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String item_name = enter_item_list.getText().toString().trim();
add_item_to_list(item_name);

}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
dialogInterface.dismiss();
AlertDialog alertDialog=builder.create();
alertDialog.show();

}
});
itemList_ref = Firestore_ref.collection("itemList").document(UserEmail).collection("user_item_list");
}
});
}

private void add_item_to_list(String item_name) {
String item_id = itemList_ref.document().getId();
InventoryModel inventoryModel = new InventoryModel(item_id,item_name ,UserName);
itemList_ref.document(item_id).set(inventoryModel).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d("TAG","Inventory list created");
}
});
}

完整代码:

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.FieldValue;
import com.google.firebase.firestore.FirebaseFirestore;
import com.omniheart.elloslai.storage_checker.model.InventoryModel;



import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

private String UserEmail , UserName ;
private GoogleApiClient googleApiClient ;
private FirebaseAuth firebase_auth;
private FirebaseFirestore Firestore_ref;
private FirebaseAuth.AuthStateListener authStateListener;
private CollectionReference itemList_ref ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GoogleSignInAccount googleSignInAccount = GoogleSignIn.getLastSignedInAccount(this);
if(googleSignInAccount!=null){
UserEmail=googleSignInAccount.getEmail();
UserName = googleSignInAccount.getDisplayName();
Toast.makeText(this,"Welcome" + UserName,Toast.LENGTH_LONG).show();
}

googleApiClient = new GoogleApiClient.Builder(this)
.addApi(Auth.GOOGLE_SIGN_IN_API)
.build();

firebase_auth=FirebaseAuth.getInstance();
Firestore_ref=FirebaseFirestore.getInstance();
authStateListener=new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser firebaseUser = firebase_auth.getCurrentUser();
if(firebaseUser == null) {
Intent intent = new Intent(MainActivity.this,LoginActivity.class);
startActivity(intent);
finish();
}
}
};
FloatingActionButton add_button = findViewById(R.id.add_item);
add_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Create item list");
final EditText enter_item_list=new EditText(MainActivity.this);
enter_item_list.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
enter_item_list.setHint("Type a name");
enter_item_list.setHintTextColor(Color.RED);
builder.setView(enter_item_list);

builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String item_name = enter_item_list.getText().toString().trim();
add_item_to_list(item_name);

}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
dialogInterface.dismiss();
AlertDialog alertDialog=builder.create();
alertDialog.show();

}
});
itemList_ref = Firestore_ref.collection("itemList").document(UserEmail).collection("user_item_list");
}
});
}

private void add_item_to_list(String item_name) {
String item_id = itemList_ref.document().getId();
InventoryModel inventoryModel = new InventoryModel(item_id,item_name ,UserName);
itemList_ref.document(item_id).set(inventoryModel).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d("TAG","Inventory list created");
}
});
}

@Override
protected void onStart() {
super.onStart();
googleApiClient.connect();
firebase_auth.addAuthStateListener(authStateListener);
}

@Override
protected void onStop() {
super.onStop();
if(googleApiClient.isConnected()){
googleApiClient.disconnect();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return true ;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.sign_out_button:
signOut() ;
return true ;

default:
return super.onOptionsItemSelected(item);
}
}

private void signOut() {
Map<String ,Object> map = new HashMap<>();
map.put("tokenId", FieldValue.delete());
Firestore_ref.collection("users").document(UserEmail).update(map).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
firebase_auth.signOut();
if (googleApiClient.isConnected()){
Auth.GoogleSignInApi.signOut(googleApiClient);
}
}
});
}


}

最佳答案

您忘记使用 alertDialog.show();显示您的 AlertDialog

检查您已将 alertDialog.show(); 放入 builder.setNegativeButton 中的代码,这就是您的原因alertDialog 未显示

像下面的代码一样更改您的代码

add_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Create item list");
final EditText enter_item_list=new EditText(MainActivity.this);
enter_item_list.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
enter_item_list.setHint("Type a name");
enter_item_list.setHintTextColor(Color.RED);
builder.setView(enter_item_list);

builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String item_name = enter_item_list.getText().toString().trim();
add_item_to_list(item_name);

}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
dialogInterface.dismiss();



}
});

AlertDialog alertDialog=builder.create();
alertDialog.show();
itemList_ref = Firestore_ref.collection("itemList").document(UserEmail).collection("user_item_list");
}
});

关于java - 警报对话框 not_showing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49298561/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com