本文介绍了Alertdialog not_showing的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是android开发的新手。试图在我的项目中实现android对话框,但它没有显示。在logcat中没有错误。
I am new to android development. tried to implement android dialog in my project but the it's not showing .There are no errors in the 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);
}
}
});
}
}
推荐答案
您忘记使用显示
You forgot to use alertDialog.show();
to display your AlertDialog
检查您的代码 alertDialog.show();
在 builder.setNegativeButton
里面为什么你的 alertDialog
没有显示
Check your code you have put alertDialog.show();
inside builder.setNegativeButton
that's why your alertDialog
is not displaying
更改你的代码如下代码
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");
}
});
这篇关于Alertdialog not_showing的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!