EditText 的使用


EditText 用來輸入編輯資料,它繼承自 TextView,許多在 TextView中的方法也可以在這使用, 可藉由 xml 來定義, 其中參數可以調整輸入資料格式,長度等等,一個 EditText 在 xml 中定義如下

   1:      <EditText
   2:          android:id="@+id/editTextTime"
   3:          android:layout_width="wrap_content"
   4:          android:layout_height="wrap_content"
   5:          android:ems="10"
   6:          android:inputType="date" >
   7:          <requestFocus />
   8:      </EditText>

第 5 行限制字數為 10 個字元
第 6 行限制輸入格式為 date

也有一些常用的方法如下

   1:          //get EditText
   2:          EditText etMoney = (EditText) findViewById(R.id.editTextMoney);
   3:          etMoney.setText("0000");
   4:          etMoney.getText().toString(); 
   5:          etMoney.setHint("提示");
   6:          etMoney.setInputType(InputType.TYPE_NULL);// disappear keyboard

特殊方法
1.
點擊 EditText 關閉原本的 keyboard, 並跳出調整日期選項,調整完再設定到 EditText 中

   1:          //get edTime
   2:          etTime = (EditText) findViewById(R.id.editTextTime);        
   3:          //get now date
   4:          final Calendar cal = Calendar.getInstance();
   5:          String dateStr = "" + cal.get(Calendar.YEAR) // 2012
   6:                  + "/" + (cal.get(Calendar.MONTH) + 1) // 12 (add 1 because it start from 0)
   7:                  + "/" + cal.get(Calendar.DATE);// 24
   8:                                                  // ....
   9:          //now date set to etTime
  10:          etTime.setText(dateStr);
  11:          // set keyboard disappear
  12:          etTime.setInputType(InputType.TYPE_NULL);
  13:          // touch etTime will show datePickDialog
  14:          etTime.setOnClickListener(new OnClickListener() 
  15:          {
  16:              
  17:              @Override
  18:              public void onClick(View v) {
  19:                  // TODO Auto-generated method stub
  20:                  
  21:                  new DatePickerDialog(ADEdit_Flow.this, new DatePickerDialog.OnDateSetListener()
  22:                  {
  23:                      
  24:                      @Override
  25:                      public void onDateSet(DatePicker view, int year, int monthOfYear,
  26:                              int dayOfMonth) {
  27:                          // TODO Auto-generated method stub
  28:                          etTime.setText(""+year+"/"+(monthOfYear+1)+"/"+dayOfMonth);
  29:                          FP.p("touch");
  30:                      }
  31:                  }, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE)).show();
  32:              }
  33:          });

2.
檢查輸入字數不可超過 7 位數,超過出現提示並重新輸入

   1:          //get EditText
   2:          EditText etMoney = (EditText) findViewById(R.id.editTextMoney);
   3:          etMoney.setText("100");
   4:          
   5:          etMoney.addTextChangedListener(new TextWatcher() {
   6:              
   7:              @Override
   8:              public void onTextChanged(CharSequence s, int start, int before, int count) {
   9:                  // TODO Auto-generated method stub
  10:                  
  11:              }
  12:              
  13:              @Override
  14:              public void beforeTextChanged(CharSequence s, int start, int count,
  15:                      int after) {
  16:                  // TODO Auto-generated method stub
  17:                  
  18:              }
  19:              
  20:              @Override
  21:              public void afterTextChanged(Editable s) {
  22:                  // TODO Auto-generated method stub
  23:                  if(s.length()>7){
  24:                      Toast.makeText(ADEdit_Flow.this, "最大輸入7位數字,請重新輸入",Toast.LENGTH_SHORT).show();
  25:                      s.clear();
  26:                  }
  27:              }
  28:          });

3.
若字串開頭部分有0,去掉0再重整,如 00987 -> 987,原理是藉由 Integer 的 parseInt() 來達成

   1:  String temps =  etMoney.getText().toString();//只要可以轉換為字串即可   
   2:  int tempi = Integer.parseInt(temps);
   3:  String.valueOf(tempi);




0 意見:

張貼留言

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Affiliate Network Reviews