Android AlertDialog未显示

时间:2017-05-02 01:01:14

标签: android android-alertdialog

我很难让AlertDialog显示出来。当用户单击textView时,alertdialog应该显示一个editText,显示textView的内容。按下保存按钮后,textview将显示editText的内容。

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class EditProfile extends AppCompatActivity {

    final DBHandler handler = new DBHandler(this);
    TextView tvPassword;
    EditText etPassword;
    String username;

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

        //Get username passed down from loginActivity
        Intent intent = getIntent();
        username = intent.getStringExtra("username");
        User user = new User();
        user = handler.getUser(username); //get user using username


        //Reference textviews with findviews
        tvPassword = (TextView) findViewById(R.id.tvPassword);


        //SetText textviews
        tvPassword.setText(user.getPassword());


        //Create alert dialogs
        AlertDialog dPassword = new AlertDialog.Builder(this).create();


        //Create editTexts to place inside dialog
        etPassword = new EditText(this);


        //Set layout for dialog
        dPassword.setView(etPassword);


        //on clicking textbox, let user edit contents
        //***PASSWORD***
        dPassword.setButton(DialogInterface.BUTTON_POSITIVE, "SAVE", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                tvPassword.setText(etPassword.getText());
            }
        });

        tvPassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                etPassword.setText(tvPassword.getText());
            }
        });


        //NOW UPDATE DB
        handler.updateUser(username, tvEmail.getText().toString(), tvPassword.getText().toString());

    }
}

2 个答案:

答案 0 :(得分:0)

您忘了拨打dPassword.show():D

答案 1 :(得分:0)

您需要显示对话框,调用dpassword.show();方法。

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class EditProfile extends AppCompatActivity { 

    final DBHandler handler = new DBHandler(this);
    TextView tvPassword;
    EditText etPassword;
    String username;

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

        //Get username passed down from loginActivity 
        Intent intent = getIntent();
        username = intent.getStringExtra("username");
        User user = new User();
        user = handler.getUser(username); //get user using username


        //Reference textviews with findviews 
        tvPassword = (TextView) findViewById(R.id.tvPassword);


        //SetText textviews 
        tvPassword.setText(user.getPassword());


        //Create alert dialogs 
        AlertDialog dPassword = new AlertDialog.Builder(this).create();


        //Create editTexts to place inside dialog 
        etPassword = new EditText(this);


        //Set layout for dialog 
        dPassword.setView(etPassword);


        //on clicking textbox, let user edit contents 
        //***PASSWORD*** 
        dPassword.setButton(DialogInterface.BUTTON_POSITIVE, "SAVE", new DialogInterface.OnClickListener() {
            @Override 
            public void onClick(DialogInterface dialog, int which) {
                tvPassword.setText(etPassword.getText());
            } 
        }); 
 dpassword.show();
        tvPassword.setOnClickListener(new View.OnClickListener() {
            @Override 
            public void onClick(View view) {
                etPassword.setText(tvPassword.getText());
            } 
        }); 


        //NOW UPDATE DB 
        handler.updateUser(username, tvEmail.getText().toString(), tvPassword.getText().toString());

    } 
}