ImageView顯示磁盤中的圖片
Uri:是一通用的資源描述路徑,可以描述文件、數據庫數據、發短信、打開地圖、打開瀏覽器等等
結構:前綴+唯一標識符+路徑
http://www.baidu.com
file:///mnt/sdcard/
顯示本地圖片
//描述本地文件的路徑
Uri uri = Uri.parse("/mnt/sdcard/pic.jpg");
ImageView iv = findViewById(R.id.iv);
//顯示圖片
iv.setImageURI(uri);
Uri也可以從File中獲取
File f =new File("/mnt/sdcard/pic.jpg");
//從File對象中獲取Uri
Uri uri = Uri.fromFile(f);
注意:從sd卡中加載圖片,需要添加sd卡權限,添加方式是在AndroidMainfest.xml中添加,如下:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xykj.day4"
android:versionCode="1"
android:versionName="1.0" >
...
<!-- 讀 sd 卡權限 -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
...
</application>
</manifest>
EditText輸入框
繼承自TextView,主要可以從鍵盤中接收用戶輸入的內容
<EditText
android:id="@+id/et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_test"
android:maxLines="3"
android:textColor="#ff0000"
android:textSize="20sp" />
<EditText
android:id="@+id/et_psw"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_psw"
android:inputType="textPassword" />
常用屬性
android:hint="@string/hint_psw" 設置輸入提示 ( 有內容時自動隱藏 )
android:inputType="textPassword" 可輸入類型:密碼、電話號碼、郵箱
android:imeOptions="actionNext" 改變 Enter 按鈕的顯示效果
actionNext 下一個、 actionGo 去往、 actionSearch 搜索、 actionDone 完成、 actionSend 發送
監聽Enter按鍵
// 按鍵監聽分爲按下、彈起
private OnEditorActionListener onEnterListener = new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent keyEvent) {
// 表示當前 Enter 按鍵彈起
// 獲取輸入框的 id
int id = v.getId();
if (id == R.id.et_name) {
// 讓輸入框得到焦點(成爲鍵盤輸入內容的目標)
etPsw.requestFocus();
} else if (id == R.id.et_psw) {
String str = etName.getText() + " " + etPsw.getText();
et.setText(str);
}
return true;
}
};
針對監聽的返回值,false表示不攔截按鍵默認功能 (Enter 默認功能是換行 ) , true 表示攔截默認功能,監聽方法中的 KeyEvent 表示按鍵的事件對象行爲包括ACTIONDOWN 、 ACTIONUP 按下彈起等行爲, KeyEvent 中還包括按鍵值
對輸入框設置Enter按鍵監聽
//輸入框監聽Enter按鍵
etName.setOnEditorActionListener(onEnterListener);
etPsw.setOnEditorActionListener(onEnterListener);
文本框內容變化監聽
可以使用文本框使用addTextChangedListener方法添加一個TextWatcher 監聽文本變化
// 監聽輸入的內容變化
etName.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int end, int count) {
Log.e("m_tag","onTextChanged:"+s);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int end,
int count) {
Log.e("m_tag","beforeTextChanged:"+s);
}
@Override
public void afterTextChanged(Editable s) {
Log.e("m_tag","afterTextChanged:"+s);
}
});
文本內容替換
輸入框中有時候需要進行內容替換,如密碼字符用小圓點來替換,或者某種需要將小寫替換爲大寫等等,需要使用 TransformationMethod 來進行處理,以密
碼字符替換爲例:
et.setTransformationMethod(PasswordTransformationMethod.getInstance());
如果需要查看實際的內容則
et.setTransformationMethod(null);
類似的還提供 SingleLineTransformationMethod 可以將換行符替換爲空 ,’\r’ 換爲空 ‘\n’ 換爲空
文本內容過濾
可以通過設置鍵盤監聽器的方式檢測輸入的內容,並且約束是否是規範的,如:
etPsw.setKeyListener(new DigitsKeyListener(){
@Override
protected char[] getAcceptedChars() {
return new char[]{'a','b','1','2'};
}
@Override
public int getInputType() {
return InputType.TYPE_TEXT_VARIATION_PASSWORD;
}
});
也可以使用系統中提供的監聽器處理
TimeKeyListener 時間類型的輸入,可以接收 0-9 、 'a' 、 'p' 、 'm' 、 ':'
實例化方式爲 TimeKeyListener.getInstance()
DigitsKeyListener 只接收數字,
如果自己有約定的規則則可以重getAcceptedChars() 方法即可
選擇框
繼承自 CompoundButton , CompoundButton 繼承自 Button ,在按鈕基礎上多了選擇狀態的指示,可以通過 checked 屬性來改變狀態,在 CompoundButton 中
提供 OnCheckedChangeListener 可以監聽單個選擇框的選擇狀態的變化
多選框
<CheckBox
android:id="@+id/ch_women"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" 美女 "
android:checked="true" />
java中使用:
// 檢測選擇框是否被選中
boolean isCheck = chCar.isChecked();
監聽狀態變化
chCar.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton btn, boolean isChecked) {
Log.e("m_tag"," 是否選中 :"+isChecked);
}
});
單選
單選按鈕RadioButton要結合單選按鈕組 (RadioGroup) 一起使用
<RadioGroup
android:id="@+id/m_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RadioButton
android:id="@+id/radio_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" 成都 " />
...
</RadioGroup>
java中使用:
RadioGroup group = (RadioGroup) findViewById(R.id.m_group);
// 獲取選擇結果
int id = group.getCheckedRadioButtonId();
監聽選中變化
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkId) {
switch (checkId) {
case R.id.radio_1:
tvResult.setText(" 恭喜你答對了 ");
break;
default:
tvResult.setText(" 回答錯誤 ");
break;
}
}
});
清楚原來的選擇
//清楚原來的選擇狀態
group.clearCheck();
// 注意,該方法調用之後會觸發 group 的監聽器中的onCheckedChanged 方法並且選擇的 id 爲 -1