纯代码实现 Android 登陆布局

下面代码实现的是 登陆 ,一点XML 配置都没有.看起来显然没有之前的使用XML配置 例子清晰.
/**
*
*/
package com.birds.android.login;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
/**
* @author birds
* @date 2008-11-22
* @file $
*/
public class LoginCodeUI extends Activity implements OnClickListener {
private static final int BUTTON_ID_OK = 5000000;
private static final int BUTTON_ID_CANCEL = 6000000;
private static final int TEXT_ID_ONE = 111111;
private static final int TEXT_PASSWD = 222222;
/*
* (non-Javadoc)
*
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TableLayout tableLayout = new TableLayout(this);
setContentView(tableLayout);
TableRow row1 = new TableRow(this);
tableLayout.addView(row1);
TextView view1 = new TextView(this);
view1.setText("账号名称:");
view1.setWidth(100);
EditText text1 = new EditText(this);
text1.setWidth(150);
text1.setSingleLine();
text1.setFilters(new InputFilter[] { new InputFilter.LengthFilter(15) });
text1.setId(TEXT_ID_ONE);
row1.addView(view1);
row1.addView(text1);
TableRow row2 = new TableRow(this);
tableLayout.addView(row2);
TextView view2 = new TextView(this);
view2.setWidth(100);
view2.setText("账号密码:");
EditText text2 = new EditText(this);
text2.setSingleLine();
text2.setWidth(150);
// 设置为密码模式
text2.setTransformationMethod(new PasswordTransformationMethod());
// 设置最大长度,为15
text2.setFilters(new InputFilter[] { new InputFilter.LengthFilter(15) });
text2.setId(TEXT_PASSWD);
row2.addView(view2);
row2.addView(text2);
TableRow row3 = new TableRow(this);
tableLayout.addView(row3);
Button b1 = new Button(this);
b1.setText("确定");
b1.setId(BUTTON_ID_OK);
b1.setOnClickListener(this);
Button b2 = new Button(this);
b2.setText("取消");
b2.setId(BUTTON_ID_CANCEL);
b2.setOnClickListener(this);
row3.addView(b1);
row3.addView(b2);
}
@Override
public void onClick(View v) {
EditText t1 = (EditText) findViewById(TEXT_ID_ONE);
EditText t2 = (EditText) findViewById(TEXT_PASSWD);
if (v.getId() == BUTTON_ID_OK) {
Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface v, int btn) {
v.cancel();
}
});
alertDialog.setTitle("输入的信息:" + t1.getText() + " 密码:"
+ t2.getText());
alertDialog.show();
} else if (v.getId() == BUTTON_ID_CANCEL) {
t1.setText("");
t2.setText("");
}
}
}
xml配置布局的灵活性明显,代码主要 是逻辑功能的实现, 有点 MVC 的思想 .
xml为视图配置,逻辑代码另写class,android事件监听 为控制器.
posted @ 2010-06-21 11:21  looky  阅读(191)  评论(0编辑  收藏  举报