My code:
package com.example.linkingpage;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class LinkingPage extends Activity {
String tv,tv1;
EditText name,pass;
TextView x,y;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.widget44);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent obj = new Intent(LinkingPage.this,LinkingPage.class);
startActivity(obj);
}
});
x = (TextView) findViewById(R.id.widget46);
y = (TextView) findViewById(R.id.widget47);
name = (EditText)findViewById(R.id.widget41);
pass = (EditText)findViewById(R.id.widget43);
Button button1 = (Button) findViewById(R.id.widget45);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
x.setText(tv);
y.setText(tv1)开发者_运维知识库;
tv = name.getText().toString();
if(tv.trim().equals("")) {
// text is empty
showDialog(EMPTY_TEXT_ALERT);
}
tv1 = pass.getText().toString();
if (tv1.trim().equals(""))
{
showDialog(EMPTY_TEXT_ALERT);
}
else
{Intent obj = new Intent(LinkingPage.this,ResourcePage.class);
obj.putExtra("name", name.getText().toString());
obj.putExtra("pass", pass.getText().toString());
startActivity(obj);
}
}
});
}
private final static int EMPTY_TEXT_ALERT = 0;
@Override
public Dialog onCreateDialog(int id) {
switch(id) {
case EMPTY_TEXT_ALERT: {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Message:Field Empty!!!")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
return builder.create();
}
}
return null;
}
}
When both fields are kept empty, and button is clicked alert is generated. Same happens when only name field is filled. But when name field is kept empty and passwor is filled , then though an alert is generated but link to other page is also generated showing my password.How can i prevent this??
Its because your your else that starts your next activity is only applying to your second if statement(the one for the password)
this:
if(tv.trim().equals("")) {
// text is empty
showDialog(EMPTY_TEXT_ALERT);
}
is completely separate from this:
if (tv1.trim().equals(""))
{
showDialog(EMPTY_TEXT_ALERT);
}else
{
Intent obj = new Intent(LinkingPage.this,ResourcePage.class);
obj.putExtra("name",name.getText().toString());
obj.putExtra("pass", pass.getText().toString());
startActivity(obj);
}
In order to make it check both of them you should do something like this:
if(tv.trim().equals("") || tv1.trim().equals(""))
{
showDialog(EMPTY_TEXT_ALERT)
}else{
Intent obj = new Intent(LinkingPage.this,ResourcePage.class);
obj.putExtra("name",name.getText().toString());
obj.putExtra("pass", pass.getText().toString());
startActivity(obj);
}
And again I really cannot stress enough how much easier your could would be to understand if you used descriptive names for your variables instead of things like tv,tv1,button,button1,widget38 etc...
Remove the code from else
and put it right there without else
there is no requirement of else
and after showDialog
put return;
and dont post duplicate posts
精彩评论