Thursday, February 6, 2014

Newbie question, EditText contents emptied problems


 package android_edittext.ycf.test; 

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

public class Android_edittextActivity extends Activity {
/ ** Called when the activity is first created. * /
@ Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.main);
Button btn1 = (Button) this.findViewById (R.id.button1);
btn1.setOnClickListener (new OnClickListener () {

public void onClick (View v) {
/ / TODO Auto-generated method stub
EditText et = (EditText) Android_edittextActivity.this
. FindViewById (R.id.edit_2);
String ettext = et.getText () toString ();.
if (ettext.length () <= 5)
{
Toast.makeText (Android_edittextActivity.this, "registered password is too short, registration failed",
. R.id.button1) show ();
et.setText ("") ;/ / ******* DDMS question in this line and below the line
Android_edittextActivity.this.setContentView (et);
}
else
{
Toast.makeText (Android_edittextActivity.this, "Congratulations on your successful registration",
. R.id.button1) show ();
}
}
});
Button btn2 = (Button) this.findViewById (R.id.button2);
btn2.setOnClickListener (new OnClickListener () {

public void onClick (View v) {
/ / TODO Auto-generated method stub
EditText et1 = (EditText) Android_edittextActivity.this
. FindViewById (R.id.edit_1);
et1.setText ("");
Android_edittextActivity.this.setContentView (et1);
EditText et2 = (EditText) Android_edittextActivity.this
. FindViewById (R.id.edit_2);
et1.setText ("");
Android_edittextActivity.this.setContentView (et2);
}
});
}
}


Novice, made an interface, two editview, one is the name of one is the password prompt if the password length is too short, and empty, but emptied two lines of code DDMS always prompt error, the error type is java.lang. IllegalStateException

Will write what is wrong
Reply:
Android_edittextActivity.this.setContentView (et2);
Android_edittextActivity.this.setContentView (et);


Will these two lines is doing what for?

Note out of it

EditText et2 = (EditText) Android_edittextActivity.this
. FindViewById (R.id.edit_2);
EditText et1 = (EditText) Android_edittextActivity.this
. FindViewById (R.id.edit_1);

The code in oncreate go inside.
Reply:
et1.setText.lenfth = 0;
Try it should be ok, I did not try
Reply:
cited a floor anelka1985 reply:
Android_edittextActivity.this.setContentView (et2);
Android_edittextActivity.this.setContentView (et);


Will these two lines is doing what for?

Note out of it

EditText et2 = (EditText) Android_edittextActivity.this
. FindViewB ......

If you remove this, how do I make him able to edittext updates on it after I modified
Reply:
Uh, you can not change the display interface in such a onClick listener function, but you can write
First in the class declaration:
Handler mHandler = new Handler ();
Runnable mRunnable = new Runnable () {

@ Override
public void run () {
mEdit.setText ("");

}
};


onClick, so call mHandler.post (mRunnable);

So no problem


Reply:
references, 4th Floor tmaccccc reply:
uh, can not change the display interface in such a onClick listener function, but you can write
First in the class declaration:
Handler mHandler = new Handler ();
Runnable mRunnable = new Runnable () {

@ Override
public void run () {
mEdit.setText ("");

}
};


onClick, so call mHandler.post (mRunnable);

So no problem

Pro-test available

No comments:

Post a Comment