我想在Android中,如果else语句中添加警告对话框 [英] I want to add alert dialog within if else statement in android

查看:102
本文介绍了我想在Android中,如果else语句中添加警告对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要提醒对话框,显示如果密码和登录ID不匹配。我尝试以下code,但是当我如果文本运行相同然后执行,但在情况下,如果密码和登录ID不匹配警报应该是显示器,而是退出进程说很遗憾项目结束。

我重视以下

我的code

 包com.example.explicitintent;进口java.security.PublicKey;进口android.os.Bundle;
进口android.app.Activity;
进口android.app.AlertDialog;
进口android.content.DialogInterface;
进口android.content.Intent;
进口android.view.Menu;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.Toast;公共类MainActivity延伸活动{
    按钮B1,B2,B3;
    的EditText E1,E2;
    字符串username =萨拉斯,密码=希腊;
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        E1 =(EditText上)findViewById(R.id.editText0001);
        E2 =(EditText上)findViewById(R.id.editText0002);
        B1 =(按钮)findViewById(R.id.button0002);
        b1.setOnClickListener(新OnClickListener(){            @覆盖
            公共无效的onClick(查看arg0中)
            {
                // TODO自动生成方法存根
                如果(username.equals(e1.getText()的toString())及。&放大器;(password.equals(e2.getText()的toString())))
                {
                    意向B =新意图(MainActivity.this,Contacts.class);
                    字符串s = e1.getText()的toString()。
                    。字符串S1 = e2.getText()的toString();
                    b.putExtra(D1,S);
                    b.putExtra(D2,S1);
                    startActivity(二);
                }
                其他
                {                    AlertDialog.Builder ALT =新AlertDialog.Builder(getApplicationContext());                    alt.setIcon(R.drawable.ic_launcher);
                    alt.setTitle(警告);
                    alt.setMessage(不要ü要重新输入密码);
                    alt.setPositiveButton(是,新DialogInterface.OnClickListener()
                    {                        @覆盖
                        公共无效的onClick(DialogInterface为arg0,ARG1 INT)
                        {
                            Toast.makeText(getApplicationContext(),OK,Toast.LENGTH_SHORT).show();                        }
                    });                    alt.setNegativeButton(否,新DialogInterface.OnClickListener()
                    {                        @覆盖
                        公共无效的onClick(DialogInterface为arg0,ARG1 INT)
                        {
                            Toast.makeText(getApplicationContext(),OK,Toast.LENGTH_SHORT).show();                        }
                    });
                    alt.show();                }
                }
        });        B2 =(按钮)findViewById(R.id.button0003);
        b2.setOnClickListener(新OnClickListener(){            @覆盖
            公共无效的onClick(查看为arg0){
                // TODO自动生成方法存根
                意向C =新意图(MainActivity.this,Reset.class);
                startActivity(C);
            }
        });        B3 =(按钮)findViewById(R.id.button1);
        b3.setOnClickListener(新OnClickListener()
        {            @覆盖
            公共无效的onClick(查看arg0中)
            {
                Toast.makeText(getApplicationContext(),保存密码,Toast.LENGTH_LONG).show();            }
        });
    }    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        //充气菜单;如果是present这增加了项目操作栏。
        。getMenuInflater()膨胀(R.menu.main,菜单);
        返回true;
    }
}


解决方案

更改此行

  AlertDialog.Builder ALT =新AlertDialog.Builder(getApplicationContext());

  AlertDialog.Builder ALT =新AlertDialog.Builder(arg0.getContext());

和你应该改变将arg0 视图 v 。如果还是不行,那么请张贴logcat的,所以我们可以看到你做了什么错误。您需要使用相应的上下文的局面,在这里你想要的 AlertDialog 使用活动 上下文这是相同的上下文你的按钮(或将arg0 )使用。

注意 MainActivity.this 将在这里做同样的事情, argo.getContext()但我最近获悉这是原因,例如,如果你想重新使用code,那么不好的做法,你必须改变code的活动名称的一部分。其访问上下文的少动的方式

<一个href=\"http://stackoverflow.com/questions/7298731/when-to-call-activity-context-or-application-context/7298955#7298955\">Here是针对上下文的问题一个很好的回答SO 。它可以是一个棘手的概念,首先要把握所以你可能要在它读几遍,并保持它附近。

I want to alert dialog to display if the password and login id doesn't match. I tried the following code, but when i run if the text are same then it executes, but in the case if the password and Login Id doesn't match an alert is supposed to be display but instead the process exits saying unfortunately your project is ended.

I have attached my code below

package com.example.explicitintent;

import java.security.PublicKey;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
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;
import android.widget.Toast;

public class MainActivity extends Activity {
    Button b1, b2,b3;
    EditText e1, e2;
    String username="saras", password="greek";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        e1 = (EditText) findViewById(R.id.editText0001);
        e2 = (EditText) findViewById(R.id.editText0002);
        b1 = (Button) findViewById(R.id.button0002);
        b1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) 
            {
                // TODO Auto-generated method stub
                if (username.equals(e1.getText().toString()) && (password.equals(e2.getText().toString())))
                {
                    Intent b = new Intent(MainActivity.this,Contacts.class);
                    String s = e1.getText().toString();
                    String s1 = e2.getText().toString();
                    b.putExtra("d1", s);
                    b.putExtra("d2", s1);
                    startActivity(b);
                }
                else
                {

                    AlertDialog.Builder alt = new AlertDialog.Builder(getApplicationContext()); 

                    alt.setIcon(R.drawable.ic_launcher);
                    alt.setTitle("WARNING");
                    alt.setMessage("Do u want to re-enter password");
                    alt.setPositiveButton("YES", new DialogInterface.OnClickListener() 
                    {

                        @Override
                        public void onClick(DialogInterface arg0, int arg1)
                        {
                            Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_SHORT).show();

                        }
                    });

                    alt.setNegativeButton("NO",new DialogInterface.OnClickListener()
                    {

                        @Override
                        public void onClick(DialogInterface arg0, int arg1) 
                        {
                            Toast.makeText(getApplicationContext(),"OK", Toast.LENGTH_SHORT).show();

                        }
                    });
                    alt.show();

                }
                }


        });

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

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent c = new Intent(MainActivity.this, Reset.class);
                startActivity(c);
            }
        });

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

            @Override
            public void onClick(View arg0) 
            {
                Toast.makeText(getApplicationContext(),"Password Saved", Toast.LENGTH_LONG).show();

            }
        });
    }

    @Override
    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;
    }




}

解决方案

Change this line

 AlertDialog.Builder alt = new AlertDialog.Builder(getApplicationContext());

to

 AlertDialog.Builder alt = new AlertDialog.Builder(arg0.getContext());

and you should change arg0 to something meaningful like view or v. If that doesn't work then please post the logcat so we can see what error you are getting. You need to use the appropriate Context for the situation and here you want the AlertDialog to use the Activity Context which is the same Context that your Button (or arg0) uses.

Note MainActivity.this will do the same thing here as argo.getContext() but I was recently informed that it is bad practice for reasons such as if you want to re-use this code then you have to change the activity name part of the code. Its a less dynamic way of accessing the Context.

Here is a good SO answer that addresses the issue of Context. It can be a tricky concept to grasp at first so you may want to read over it a few times and keep it close by.

这篇关于我想在Android中,如果else语句中添加警告对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆