Android登录界面设计1

Android登录界面设计1:

  1. import android.app.Activity;  
  2. import android.os.Bundle;  
  3. import android.text.Editable;  
  4. import android.text.InputType;  
  5. import android.text.TextWatcher;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.*;  
  9.   
  10. public class LoginUI1 extends Activity {  
  11.      
  12.     private LinearLayout mainLayout=null;  
  13.     private RelativeLayout layout1=null;      
  14.     private TextView tv11=null;     //请输入账号        
  15.     private TextView tv12=null;     //显示账号可输入剩余字符数   
  16.     private EditText et1=null;      //账号框   
  17.     private RelativeLayout layout2=null;  
  18.     private TextView tv21=null;     //请输入密码   
  19.     private TextView tv22=null;     //显示密码可输入剩余字符数   
  20.     private EditText et2=null;      //密码输入框   
  21.     private LinearLayout layout3=null;  
  22.     private Button button1=null;    //按钮1:取消   
  23.     private Button button2=null;    //按钮2:登陆   
  24.       
  25.     public void onCreate(Bundle savedInstanceState) {  
  26.         super.onCreate(savedInstanceState);  
  27.         mainLayout_init();  
  28.         setContentView(mainLayout);  
  29.     }  
  30.     /*mainLayout初始化*/  
  31.     void mainLayout_init(){  
  32.         mainLayout=new LinearLayout(this);  
  33.         LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(-1, -1);  
  34.         mainLayout.setLayoutParams(lp);  
  35.         mainLayout.setOrientation(LinearLayout.VERTICAL);  
  36.         layout1_init();  
  37.         mainLayout.addView(layout1);  
  38.         et1_init();  
  39.         mainLayout.addView(et1);  
  40.         layout2_init();  
  41.         mainLayout.addView(layout2);  
  42.         et2_init();  
  43.         mainLayout.addView(et2);  
  44.         layout3_init();  
  45.         mainLayout.addView(layout3);  
  46.     }  
  47.     /*layout1初始化*/  
  48.     void layout1_init(){  
  49.         layout1=new RelativeLayout(this);  
  50.         RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(-1, -2);  
  51.         layout1.setLayoutParams(lp);  
  52.         tv11_init();  
  53.         layout1.addView(tv11);  
  54.         tv12_init();  
  55.         layout1.addView(tv12);  
  56.     }  
  57.     /*tv11初始化*/  
  58.     void tv11_init(){  
  59.         tv11=new TextView(this);  
  60.         RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(-2, -2);  
  61.         lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);  
  62.         tv11.setLayoutParams(lp);  
  63.         tv11.setTextSize(30);  
  64.         tv11.setText("请输入账号:");  
  65.     }  
  66.     /*tv12初始化*/  
  67.     void tv12_init(){  
  68.         tv12=new TextView(this);  
  69.         RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(-2, -2);  
  70.         lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);  
  71.         tv12.setLayoutParams(lp);  
  72.         tv12.setTextSize(30);  
  73.         tv12.setText("12");  
  74.     }  
  75.     /*et1初始化*/  
  76.     void et1_init(){  
  77.         et1=new EditText(this);  
  78.         LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(-1,-2);  
  79.         et1.setLayoutParams(lp);  
  80.         et1.setSingleLine(true);  
  81.         TextWatcher tw=new TextWatcher(){  
  82.             public void afterTextChanged(Editable s) {  
  83.                 //限制输入   
  84.                 if(et1.length()>12){  
  85.                     //截取字符串,舍弃最后一个   
  86.                     et1.setText((et1.getText()).subSequence(012));  
  87.                     //设置光标。由于用setText函数会导致光标复位,所以重新设置光标到末尾   
  88.                     et1.setSelection(12);  
  89.                 }  
  90.                 //显示剩余可输入字符数   
  91.                 tv12.setText(String.valueOf(12-et1.length()));  
  92.             }  
  93.             public void beforeTextChanged(CharSequence s, int start, int count,int after) {}  
  94.             public void onTextChanged(CharSequence s, int start, int before,int count) {}  
  95.         };  
  96.         et1.addTextChangedListener(tw);  
  97.     }  
  98.     /*layout2初始化*/  
  99.     void layout2_init(){  
  100.         layout2=new RelativeLayout(this);  
  101.         LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(-1, -2);  
  102.         layout2.setLayoutParams(lp);  
  103.         tv21_init();  
  104.         layout2.addView(tv21);  
  105.         tv22_init();  
  106.         layout2.addView(tv22);  
  107.     }  
  108.     /*tv21初始化*/  
  109.     void tv21_init(){  
  110.         tv21=new TextView(this);  
  111.         RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(-2, -2);  
  112.         lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);  
  113.         tv21.setLayoutParams(lp);  
  114.         tv21.setTextSize(30);  
  115.         tv21.setText("请输入密码:");  
  116.     }  
  117.     /*tv22初始化*/  
  118.     void tv22_init(){  
  119.         tv22=new TextView(this);  
  120.         RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(-2, -2);  
  121.         lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);  
  122.         tv22.setLayoutParams(lp);  
  123.         tv22.setTextSize(30);  
  124.         tv22.setText("12");  
  125.     }  
  126.     /*et2初始化*/  
  127.     void et2_init(){  
  128.         et2=new EditText(this);  
  129.         LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(-1, -2);  
  130.         et2.setLayoutParams(lp);  
  131.         et2.setSingleLine();  
  132.         //设置输入模式为密码模式   
  133.         et2.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD);  
  134.         TextWatcher tw=new TextWatcher(){  
  135.             public void afterTextChanged(Editable s) {  
  136.                 //限制输入   
  137.                 if(et2.length()>12){  
  138.                     //截取字符串,舍弃最后一个   
  139.                     et2.setText((et2.getText()).subSequence(012));  
  140.                     //设置光标。由于用setText函数会导致光标复位,所以重新设置光标到末尾   
  141.                     et2.setSelection(12);  
  142.                 }  
  143.                 //显示剩余可输入字符数   
  144.                 tv22.setText(String.valueOf(12-et2.length()));  
  145.             }  
  146.             public void beforeTextChanged(CharSequence s, int start, int count,int after) {}  
  147.             public void onTextChanged(CharSequence s, int start, int before,int count) {}  
  148.         };  
  149.         et2.addTextChangedListener(tw);  
  150.     }  
  151.     /*layout3初始化*/  
  152.     void layout3_init(){  
  153.         layout3=new LinearLayout(this);  
  154.         LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(-1,-2);  
  155.         layout3.setLayoutParams(lp);  
  156.         layout3.setOrientation(LinearLayout.HORIZONTAL);  
  157.         button1_init();  
  158.         layout3.addView(button1);  
  159.         button2_init();  
  160.         layout3.addView(button2);  
  161.     }  
  162.     /*button1初始化*/  
  163.     void button1_init(){  
  164.         button1=new Button(this);  
  165.         LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(-2, -2);  
  166.         button1.setLayoutParams(lp);  
  167.         button1.setTextSize(30);  
  168.         button1.setText("取消");  
  169.         OnClickListener ol=new OnClickListener(){  
  170.             public void onClick(View v) {  
  171.                 //此处做“取消”事件处理   
  172.             }  
  173.         };  
  174.         button1.setOnClickListener(ol);  
  175.     }  
  176.     /*button2初始化*/  
  177.     void button2_init(){  
  178.         button2=new Button(this);  
  179.         LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(-1, -2);  
  180.         button2.setLayoutParams(lp);  
  181.         button2.setTextSize(30);  
  182.         button2.setText("登录");  
  183.         OnClickListener ol=new OnClickListener(){  
  184.             public void onClick(View v) {  
  185.                 //此处做“登陆”事件处理   
  186.             }  
  187.         };  
  188.         button2.setOnClickListener(ol);  
  189.     }  
  190.       
  191. }  

Android登录界面设计1

相关推荐