安卓界面开发纯粹使用代码设计模式
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
public class LoginUI1 extends Activity {
private LinearLayout mainLayout=null;
private RelativeLayout layout1=null;
private TextView tv11=null; //请输入账号
private TextView tv12=null; //显示账号可输入剩余字符数
private EditText et1=null; //账号框
private RelativeLayout layout2=null;
private TextView tv21=null; //请输入密码
private TextView tv22=null; //显示密码可输入剩余字符数
private EditText et2=null; //密码输入框
private LinearLayout layout3=null;
private Button button1=null; //按钮1:取消
private Button button2=null; //按钮2:登陆
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mainLayout_init();
setContentView(mainLayout);
}
/*mainLayout初始化*/
void mainLayout_init(){
mainLayout=new LinearLayout(this);
LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(-1, -1);
mainLayout.setLayoutParams(lp);
mainLayout.setOrientation(LinearLayout.VERTICAL);
layout1_init();
mainLayout.addView(layout1);
et1_init();
mainLayout.addView(et1);
layout2_init();
mainLayout.addView(layout2);
et2_init();
mainLayout.addView(et2);
layout3_init();
mainLayout.addView(layout3);
}
/*layout1初始化*/
void layout1_init(){
layout1=new RelativeLayout(this);
RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(-1, -2);
layout1.setLayoutParams(lp);
tv11_init();
layout1.addView(tv11);
tv12_init();
layout1.addView(tv12);
}
/*tv11初始化*/
void tv11_init(){
tv11=new TextView(this);
RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(-2, -2);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
tv11.setLayoutParams(lp);
tv11.setTextSize(30);
tv11.setText("请输入账号:");
}
/*tv12初始化*/
void tv12_init(){
tv12=new TextView(this);
RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(-2, -2);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
tv12.setLayoutParams(lp);
tv12.setTextSize(30);
tv12.setText("12");
}
/*et1初始化*/
void et1_init(){
et1=new EditText(this);
LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(-1,-2);
et1.setLayoutParams(lp);
et1.setSingleLine(true);
TextWatcher tw=new TextWatcher(){
public void afterTextChanged(Editable s) {
//限制输入
if(et1.length()>12){
//截取字符串,舍弃最后一个
et1.setText((et1.getText()).subSequence(0, 12));
//设置光标。由于用setText函数会导致光标复位,所以重新设置光标到末尾
et1.setSelection(12);
}
//显示剩余可输入字符数
tv12.setText(String.valueOf(12-et1.length()));
}
public void beforeTextChanged(CharSequence s, int start, int count,int after) {}
public void onTextChanged(CharSequence s, int start, int before,int count) {}
};
et1.addTextChangedListener(tw);
}
/*layout2初始化*/
void layout2_init(){
layout2=new RelativeLayout(this);
LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(-1, -2);
layout2.setLayoutParams(lp);
tv21_init();
layout2.addView(tv21);
tv22_init();
layout2.addView(tv22);
}
/*tv21初始化*/
void tv21_init(){
tv21=new TextView(this);
RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(-2, -2);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
tv21.setLayoutParams(lp);
tv21.setTextSize(30);
tv21.setText("请输入密码:");
}
/*tv22初始化*/
void tv22_init(){
tv22=new TextView(this);
RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(-2, -2);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
tv22.setLayoutParams(lp);
tv22.setTextSize(30);
tv22.setText("12");
}
/*et2初始化*/
void et2_init(){
et2=new EditText(this);
LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(-1, -2);
et2.setLayoutParams(lp);
et2.setSingleLine();
//设置输入模式为密码模式
et2.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD);
TextWatcher tw=new TextWatcher(){
public void afterTextChanged(Editable s) {
//限制输入
if(et2.length()>12){
//截取字符串,舍弃最后一个
et2.setText((et2.getText()).subSequence(0, 12));
//设置光标。由于用setText函数会导致光标复位,所以重新设置光标到末尾
et2.setSelection(12);
}
//显示剩余可输入字符数
tv22.setText(String.valueOf(12-et2.length()));
}
public void beforeTextChanged(CharSequence s, int start, int count,int after) {}
public void onTextChanged(CharSequence s, int start, int before,int count) {}
};
et2.addTextChangedListener(tw);
}
/*layout3初始化*/
void layout3_init(){
layout3=new LinearLayout(this);
LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(-1,-2);
layout3.setLayoutParams(lp);
layout3.setOrientation(LinearLayout.HORIZONTAL);
button1_init();
layout3.addView(button1);
button2_init();
layout3.addView(button2);
}
/*button1初始化*/
void button1_init(){
button1=new Button(this);
LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(-2, -2);
button1.setLayoutParams(lp);
button1.setTextSize(30);
button1.setText("取消");
OnClickListener ol=new OnClickListener(){
public void onClick(View v) {
//此处做“取消”事件处理
}
};
button1.setOnClickListener(ol);
}
/*button2初始化*/
void button2_init(){
button2=new Button(this);
LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(-1, -2);
button2.setLayoutParams(lp);
button2.setTextSize(30);
button2.setText("登录");
OnClickListener ol=new OnClickListener(){
public void onClick(View v) {
//此处做“登陆”事件处理
}
};
button2.setOnClickListener(ol);
}
}
阅读(5285) | 评论(0) | 转发(0) |