开发者

Regarding Validation of Edit Text in Android

I am working on validation of two edit text fields in my login page and little change in my idea. I need to show textview above this field if the user left field e1 without entering text and goes to e2. Any help is appreciated.

Here is my code:

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

;
public class LoginActivity extends Activity {
    EditText usernameText;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedIns开发者_开发知识库tanceState);
        setContentView(R.layout.loginpage);
        Button login = (Button) findViewById(R.id.loghomebutton);
        Button newuser = (Button) findViewById(R.id.lognewuserbutton);
        EditText usernameText = (EditText)this.findViewById(R.id.emaileditlog);
        EditText passwordText = (EditText)this.findViewById(R.id.pwdeditlog);


    login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent myIntent = new Intent(v.getContext(),
                    SettingsActivity.class);
            startActivityForResult(myIntent, 0);
            overridePendingTransition(R.anim.fadeout, 0);
        }
    });
    newuser.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent myIntent = new Intent(v.getContext(),
                    RegisterActivity.class);
            startActivityForResult(myIntent, 1);
            overridePendingTransition(R.anim.fadeout, 0);
        }
    });

}

}


in XML add a textview above the edittext.. set textviews android:visibility="GONE" then in java code do this..

if(editText.getText.toString().equals(" "))
                   {
                   TextView t = findViewById(.......)
                   t.setVisibility(View.VISIBLE);
                   t.setText("..............");
                   }


In your xml layout, define your textview, place it as you want, then use

android:visibility="gone" 

and in your code, when needed

TextView errorTextView = (TextView) findViewById( R.id.error_view );
errorTextView.setVisibility( View.VISIBLE );

Regards, Stéphane


It would actually be better if you used the hint feature of the editbox.

Here is how it will look with no data:

Regarding Validation of Edit Text in Android


I have used like this in my Calculator application.I am showing an Alertdialog instead of diaplaying error message in Textbox

if(calc_txt_Prise.getEditableText().toString().trim().equals(""){
String dialog_message = "Please enter some Text";
                         AlertDialog.Builder invalid_input_dialog = new     AlertDialog.Builder(Archive.this);
                          invalid_input_dialog.setTitle("Application name")
                          .setMessage(dialog_message)
                          .setCancelable(true)
                          .setPositiveButton("Yes", new DialogInterface.OnClickListener(){
                              @Override
                                public void onClick(DialogInterface dialog, int which) {
                                  showDialog(DATE_DIALOG_ID);
                              }
                          })
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜