Java codepackage 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, "注册密码太短,注册失败",
R.id.button1).show();
et.setText("");//*******DDMS提示问题出现在这行和下面一行
Android_edittextActivity.this.setContentView(et);
}
else
{
Toast.makeText(Android_edittextActivity.this, "恭喜您注册成功",
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);
}
});
}
}
菜鸟问题,做了一个界面,两个editview,一个是姓名一个是密码,如果密码长度太短进行提示,并且清空,可是清空的这两行代码DDMS总是提示错误,错误类型为java.lang.IllegalStateException
请问这样写是不对的么
Android_edittextActivity.this.setContentView(et2);
Android_edittextActivity.this.setContentView(et);
想请问这2行是做什么用的?
注掉吧
EditText et2 = (EditText) Android_edittextActivity.this
.findViewById(R.id.edit_2);
EditText et1 = (EditText) Android_edittextActivity.this
.findViewById(R.id.edit_1);
这些代码放在oncreate里边去吧。
呃,不能在onClick这样的监听函数中改变界面的显示,但可以这样写
先在类中声明:
Handler mHandler = new Handler();
Runnable mRunnable = new Runnable() {
@Override
public void run() {
mEdit.setText("");
}};
onClick中,这样调用mHandler.post(mRunnable);
这样就没问题了