gpt4 book ai didi

android - 我想在 if else 语句中添加警告对话框

转载 作者:行者123 更新时间:2023-11-29 17:57:18 24 4
gpt4 key购买 nike

如果密码和登录 ID 不匹配,我想显示警告对话框。我尝试了以下代码,但是当我运行时如果文本相同则执行,但是如果密码和登录 ID 不匹配应该显示警报但进程退出说不幸的是你的项目是结束了。

我在下面附上了我的代码

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




}

最佳答案

改变这一行

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

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

并且您应该将 arg0 更改为有意义的内容,例如 viewv。如果这不起作用,请发布 logcat,以便我们查看您遇到的错误。您需要根据情况使用适当的 Context,在这里您希望 AlertDialog 使用 Activity Context与您的 Button(或 arg0)使用的 Context 相同。

注意 MainActivity.this 将在这里做与 argo.getContext() 相同的事情,但我最近被告知这是不好的做法出于诸如如果您想重新使用此代码的原因,您必须更改代码的 Activity 名称部分。这是访问 Context 的一种不太动态的方式。

Here is a good SO answer解决了 Context 的问题。一开始这可能是一个难以掌握的概念,因此您可能需要多读几遍并将其放在身边。

关于android - 我想在 if else 语句中添加警告对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18471959/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com