tags:

views:

28

answers:

1

I want to program a function that the toast exist when there is nothing in the "edittext" box (id / password), but it dosen't work.

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

public class Test extends Activity
{

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button button1 = (Button) findViewById(R.id.button1);
    EditText id = (EditText) findViewById(R.id.id);
    EditText pwd = (EditText) findViewById(R.id.pwd);
    Editable Str_id;
    Editable Str_pwd;
    Str_id = id.getText();
    Str_pwd = pwd.getText();



    button1.setOnClickListener(new Button.OnClickListener()
    {

      public void onClick(View v){
        if(Str_id==null || Str_pwd == null){
        Toast.makeText(Test.this, "Please Enter User ID / Password", Toast.LENGTH_LONG).show();

     }
    });
    }



  }
A: 

move the following lines inside onClick and try:

Str_id = id.getText(); 
Str_pwd = pwd.getText(); 

EDIT: Move

Editable Str_id; 
Editable Str_pwd; 
Str_id = id.getText(); 
Str_pwd = pwd.getText();

inside onClick().

Samuh
When the onCreate() is executing the views are not even drawn so, getText() in onCreate wouldnt work or should at least yield unexepected behavior.
Samuh
Error, it recommend to "change modifier of "id" and "pwd" to final.
Hello Boy
depending on your need you can do any of the following:1. move the declarations of those fields inside onClick() or2. Make those fields as instance variables of the class by moving them outside onCreate.
Samuh