2016年7月23日 星期六

uidemo

Ref :  Android App 開發教戰手冊  ch4

EditText 的輸入type 有很多種...


android:inputType="textPassword"   ->  密碼



android:inputType="phone"         ->   phone

android:inputType="number"         ->   數字





<EditText    android:id="@+id/etUser"    android:hint="@string/hint_etUser"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_marginTop="34dp" />

<EditText    android:id="@+id/etPassword"    android:hint="@string/hint_etPassword"    android:inputType="textPassword"    android:layout_width="match_parent"    android:layout_height="wrap_content" />

<EditText    android:id="@+id/etPhone"    android:hint="@string/hint_etPhone"    android:inputType="phone"    android:layout_width="match_parent"    android:layout_height="wrap_content" />

<EditText    android:id="@+id/etAge"    android:hint="@string/hint_etAge"    android:inputType="number"    android:layout_width="match_parent"    android:layout_height="wrap_content" />

<Button    android:id="@+id/btSubmit"    android:onClick="onSubmitClick"    android:text="@string/text_btSubmit"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_marginTop="10dp" />

<Button    android:id="@+id/btClear"    android:text="@string/text_btClear"    android:layout_width="match_parent"    android:layout_height="wrap_content" />

<TextView    android:id="@+id/tvMessage"    android:lines="5"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_marginTop="10dp"    android:background="#DDDDFF" />



========================================================
main.java   

get string 可以這樣用

 String user = etUser.getText().toString().trim();

.trim() 是去除不必要的空白


========================================================


@Overrideprotected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    findViews();
}

private void findViews() {
    etUser = (EditText) findViewById(R.id.etUser);
    etPassword = (EditText) findViewById(R.id.etPassword);
    etPhone = (EditText) findViewById(R.id.etPhone);
    etAge = (EditText) findViewById(R.id.etAge);
    btClear = (Button) findViewById(R.id.btClear);
    tvMessage = (TextView) findViewById(R.id.tvMessage);

    btClear.setOnClickListener(new View.OnClickListener() {
        @Override        public void onClick(View v) {
            etUser.setText(null);
            etPassword.setText(null);
            etPhone.setText(null);
            etAge.setText(null);
            tvMessage.setText(null);
            Toast.makeText(
                    MainActivity.this,
                    R.string.msg_ClearAllFields,
                    Toast.LENGTH_SHORT            ).show();
        }
    });
}

public void onSubmitClick(View view) {
    String user = etUser.getText().toString().trim();
    String password = etPassword.getText().toString().trim();
    String phone = etPhone.getText().toString().trim();
    String age = etAge.getText().toString().trim();
    String text = "";
    text += "user name = " + user + "\n";
    text += "password = " + password + "\n";
    text += "phone number = " + phone + "\n";
    text += "age = " + age + "\n";
    tvMessage.setText(text);
}

沒有留言:

張貼留言