当前位置: 首页 > 面试题库 >

Alertdialog not_showing

苏坚成
2023-03-14
问题内容

我是android开发的新手。试图在我的项目中实现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");
        }
    });


 类似资料:

相关阅读

相关文章

相关问答