想要在startActivity,android之前要求用户确认他的信息是否正确

时间:2014-09-25 04:09:47

标签: java android dialog

您好,我是Android开发中的新手。 所以我练习了一个样本,即向运营商发送ussd代码,如充值账单。我没关系。

但我的主要问题是在填写PIN码后点击button1时显示对话框,以检查用户是否正确。 我不知道如何添加和添加内容。

我的代码是 -

MainActivity.java

package com.messagealert;

import android.net.Uri;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

 [MENTION=439709]override[/MENTION]
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new OnClickListener(){
            public void onClick (View v){
                EditText editText1 =(EditText) findViewById(R.id.editText1);
                String pin = editText1.getText().toString();
                String encodedHash = Uri.encode("#");
                String ussd = "*" + "124" + pin + encodedHash;
                Intent button1 = new Intent(android.content.Intent.ACTION_CALL, Uri.parse("tel:" + ussd));      
                startActivity(button1);
            }
        });

    }

 [MENTION=439709]override[/MENTION]
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

activity_main.xml

<RelativeLayout xmlns:android="h t t p://schemas. android. com/apk/res/android"
    xmlns:tools="h t t p:// schemas. android. com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="22dp"
        android:layout_centerHorizontal="true"
        android:ems="10"
        android:inputType="number"
        android:maxLength="6"
        android:hint="Enter 6 numbers here"
         >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="31dp"
        android:layout_centerHorizontal="true"
        android:text="@string/button1" />

</RelativeLayout>

请指导我需要放的东西&amp;在哪里准确显示对话框这是您的PIN码 - xxxxxx ”&amp;

抱歉我的英语不好。

最诚挚的问候,

6 个答案:

答案 0 :(得分:1)

      Button button1 = (Button) findViewById(R.id.button1);
            button1.setOnClickListener(new OnClickListener(){
                public void onClick (View v){
                    EditText editText1 =(EditText) findViewById(R.id.editText1);
                    String pin = editText1.getText().toString();

     AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(this);

                        dlgAlert.setMessage("PIN VERIFICATION");
                        dlgAlert.setTitle("Is that your PIN code -"+pin );
                        dlgAlert.setPositiveButton("OK", null);
                        dlgAlert.setCancelable(true);
                        dlgAlert.create().show();

                        dlgAlert.setPositiveButton("Yes",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
      String encodedHash = Uri.encode("#");
                    String ussd = "*" + "124" + pin + encodedHash;
                    Intent button1 = new Intent(android.content.Intent.ACTION_CALL, Uri.parse("tel:" + ussd));      
                    startActivity(button1);


                            }
                        });
  dlgAlert.setNegativeButton("NO",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {

                  editText1 .setText(" ");
                }
            });

答案 1 :(得分:0)

为此,您必须添加警告框,如下面的代码

new AlertDialog.Builder(this)
    .setTitle("Delete entry")
    .setMessage("Are you sure you want to delete this entry?")
    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
            // continue with delete
        }
     })
    .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
            // do nothing
        }
     })
    .setIcon(android.R.drawable.ic_dialog_alert)
     .show();

或试试这个

AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
            builder1.setMessage("Write your message here.");
            builder1.setCancelable(true);
            builder1.setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
            builder1.setNegativeButton("No",
                    new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });

            AlertDialog alert11 = builder1.create();
            alert11.show();

答案 2 :(得分:0)

只需在按钮中添加对话框,只需点击

 EditText editText1 =(EditText) findViewById(R.id.editText1);

Button button1 = (Button) findViewById(R.id.button1);
    button1.setOnClickListener(new OnClickListener(){
        public void onClick (View v){


           //call dialog here and on dialog ok press start your activity

            String pin = editText1.getText().toString();
            String encodedHash = Uri.encode("#");
            String ussd = "*" + "124" + pin + encodedHash;
            Intent button1 = new Intent(android.content.Intent.ACTION_CALL, Uri.parse("tel:" + ussd));      
            startActivity(button1);
        }
    });

答案 3 :(得分:0)

使用以下代码,这将对您有所帮助:

     Button button1 = (Button) findViewById(R.id.button1);
    button1.setOnClickListener(new OnClickListener(){
        public void onClick (View v){
           dialog();
        }
    });


   public void dialog()
 {
     AlertDialog.Builder dialog = new Builder(con);

     dialog.setTitle("Message:");
    dialog.setMessage("Is that your PIN code - xxxxxx");
     dialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
        dialog.dismiss();   
        }
    });
     dialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
             EditText editText1 =(EditText) findViewById(R.id.editText1);
            String pin = editText1.getText().toString();
            String encodedHash = Uri.encode("#");
            String ussd = "*" + "124" + pin + encodedHash;
            Intent button1 = new Intent(android.content.Intent.ACTION_CALL, Uri.parse("tel:" + ussd));      
            startActivity(button1);
            dialog.dismiss();
        }
    });
     dialog.show();
 }

如果需要,可以使用变量global。

答案 4 :(得分:0)

使用alertDialog为你点击按钮onClick

AlertDialog.Builder dialog = new Builder(con);

 dialog.setTitle("Confirm PIN");
dialog.setMessage("You have entered pincode as"+pin+"\n Please Confirm to Continue");
 dialog.setNegativeButton("Edit Pin", new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
    dialog.dismiss();   
    }
});
 dialog.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
         EditText editText1 =(EditText) findViewById(R.id.editText1);
        String pin = editText1.getText().toString();
        String encodedHash = Uri.encode("#");
        String ussd = "*" + "124" + pin + encodedHash;
        Intent button1 = new Intent(android.content.Intent.ACTION_CALL, Uri.parse("tel:" + ussd));      
            startActivity(button1);
        dialog.dismiss();
    }
});
 dialog.show();

答案 5 :(得分:0)

首先,我非常感谢你的回答。 对不起,我无法对你的答案进行投票,因为我没有足够的声誉。

通过调整MSS的答案我改变了一点 MainActivity.java

package com.messagealert;

import android.net.Uri;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

    final Context context = this;

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

        Button button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new OnClickListener(){


            public void onClick (View v){
                EditText editText1 =(EditText) findViewById(R.id.editText1);
                String pin = editText1.getText().toString();

                AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(context);

                    dlgAlert.setTitle("PIN VERIFICATION");
                    dlgAlert.setMessage("Is that your PIN code -" + pin );



                    dlgAlert.setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                String encodedHash = Uri.encode("#");
                EditText editText1 =(EditText) findViewById(R.id.editText1);
                String pin = editText1.getText().toString();
                String ussd = "*" + "124" + pin + encodedHash;
                Intent button1 = new Intent(android.content.Intent.ACTION_CALL, Uri.parse("tel:" + ussd));      
                startActivity(button1);


                        }
                    });
dlgAlert.setNegativeButton("NO",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            EditText editText1 =(EditText) findViewById(R.id.editText1);
              editText1 .setText(" ");
            }
        });

dlgAlert.setCancelable(true);
dlgAlert.create().show();

            };
        });
 }}

你们这些人对我有很大的帮助!

关心,