Android入门之EditText(纯Java)


  1. import Android.app.Activity;  
  2. import android.os.Bundle;  
  3. import android.text.Editable;  
  4. import android.text.TextWatcher;  
  5. import android.widget.*;  
  6.   
  7. public class EditTextTest extends Activity {  
  8.      
  9.     private LinearLayout mainLayout=null;  
  10.       
  11.     private RelativeLayout layout=null;   
  12.     private TextView tv1=null;      //标题           
  13.     private TextView tv2=null;      //可输入剩余字符数   
  14.     private EditText et=null;       //输入框          
  15.       
  16.     public void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         mainLayout_init();  
  19.         setContentView(mainLayout);  
  20.     }  
  21.     /*mainLayout初始化*/  
  22.     void mainLayout_init(){  
  23.         mainLayout=new LinearLayout(this);  
  24.         LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(-1, -1);  
  25.         mainLayout.setLayoutParams(lp);  
  26.         mainLayout.setOrientation(LinearLayout.VERTICAL);  
  27.         layout_init();  
  28.         mainLayout.addView(layout);  
  29.         et_init();  
  30.         mainLayout.addView(et);  
  31.     }  
  32.     /*layout1初始化*/  
  33.     void layout_init(){  
  34.         layout=new RelativeLayout(this);  
  35.         RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(-1, -2);  
  36.         layout.setLayoutParams(lp);  
  37.         tv1_init();  
  38.         layout.addView(tv1);  
  39.         tv2_init();  
  40.         layout.addView(tv2);  
  41.     }  
  42.     /*tv11初始化*/  
  43.     void tv1_init(){  
  44.         tv1=new TextView(this);  
  45.         RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(-2, -2);  
  46.         lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);  
  47.         tv1.setLayoutParams(lp);  
  48.         tv1.setTextSize(30);  
  49.         tv1.setText("请输入账号:");  
  50.     }  
  51.     /*tv12初始化*/  
  52.     void tv2_init(){  
  53.         tv2=new TextView(this);  
  54.         RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(-2, -2);  
  55.         lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);  
  56.         tv2.setLayoutParams(lp);  
  57.         tv2.setTextSize(30);  
  58.         tv2.setText("12");  
  59.     }  
  60.     /*et1初始化*/  
  61.     void et_init(){  
  62.         et=new EditText(this);  
  63.         LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(-1,-2);  
  64.         et.setLayoutParams(lp);  
  65.         et.setSingleLine(true);  
  66.         //字符变化监听   
  67.         TextWatcher tw=new TextWatcher(){  
  68.             public void afterTextChanged(Editable s) {  
  69.                 //限制输入,最多允许输入12个   
  70.                 if(et.length()>12){  
  71.                     //截取字符串,舍弃最后一个   
  72.                     et.setText((et.getText()).subSequence(012));  
  73.                     //设置光标。由于用setText函数会导致光标复位,所以重新设置光标到末尾   
  74.                     et.setSelection(12);  
  75.                 }  
  76.                 //显示剩余可输入字符数   
  77.                 tv2.setText(String.valueOf(12-et.length()));  
  78.             }  
  79.             public void beforeTextChanged(CharSequence s, int start, int count,int after) {}  
  80.             public void onTextChanged(CharSequence s, int start, int before,int count) {}  
  81.         };  
  82.         et.addTextChangedListener(tw);  
  83.     }  
  84. }  

相关内容