快速連結

2012年10月18日

Android~第一次使用EditText

本次要利用EditText與得到系統時間的方式來得知輸入者的生日年份~!




先到介面把TextFields抓出你想要的TextField~

如果要增加提示文字
然後在strings.xml內輸入文字,在activity_main.xml內將他設定在hint內。

style="@style/styleName" 設定顯示的Style,可以去styles.xml去撰寫。
android:hint="@string/EditAge" 設定顯示的提示文字
android:inputType="number" 設定輸入的文字類型
android:maxLength="3" 輸入的最大長度

接著跳到MainActivity.java內,寫上程式碼~


package com.example.helloworld;
....
....
import android.widget.EditText;
import android.widget.TextView.OnEditorActionListener;

....
....

final EditText editText=(EditText)findViewById(R.id.txtAge);  
        editText.setOnEditorActionListener(new OnEditorActionListener() {  
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
             int age = Integer.parseInt(editText.getText().toString()); //轉成數字
             Time t = new Time();  //取得系統時間~
             t.setToNow(); //設定是現在的時間
             int nowYear = t.year; //取得年份~
             int year = nowYear-age;
             txtShow.setText("你的年紀是"+ age +"歲,出生年為"+year+"年");
                return false;  
            }  
        });  








沒有留言:

張貼留言

歡迎大家留言提問,我會答的都會盡力回答!
如果太久沒出現回應就是我又忘記回來看留言了TAT