在 Android開(kāi)發(fā)之那些好用的數(shù)據(jù)結(jié)構(gòu)與API 一文中提到了Android中一些好用的數(shù)據(jù)結(jié)構(gòu)和API,這次繼續(xù)補(bǔ)充幾個(gè)我在項(xiàng)目中用到的好用的但是不是人人都知道的東東 ~~
</br>
1、android:digits
在Android開(kāi)發(fā)中,經(jīng)常要設(shè)置EditText為密碼顯示,但是通常要求密碼只能是 **字母和數(shù)字 . _ **的組合,此時(shí)就可以用該屬性進(jìn)行過(guò)濾
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:digits="0123456789abcdefghigklmnopqrstuvwxyzQWERTYUIOPASDFGHJKLZXCVBNM._" />
測(cè)試效果

2、setKeyListener()
接著上面說(shuō),還有一種方法也可以限定EditText輸入字符,那就是給EditText設(shè)置KeyListener
et.setKeyListener(new NumberKeyListener() {
//限制彈出的鍵盤類型
public int getInputType() {
return InputType.TYPE_CLASS_NUMBER;
}
//限定輸入的字符
protected char[] getAcceptedChars() {
char[] numbers = new char[]{'.', '1', '3', '5', '7', '9'};
return numbers;
}
});
測(cè)試效果

3、ListView 的 setEmptyView
該方法可以為沒(méi)有數(shù)據(jù)的ListView 設(shè)置一個(gè)提示View,常常用在ListView沒(méi)有加載到數(shù)據(jù)或加載數(shù)據(jù)失敗時(shí)提示
布局 (需要準(zhǔn)備一個(gè)背景透明的提示圖片)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#123456" />
<ImageView
android:id="@+id/empty"
android:layout_width="match_parent "
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:src="@mipmap/nomorenomal" />
</RelativeLayout>
Activity
public class MainActivity extends AppCompatActivity {
private ListView mliListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mliListView = (ListView) findViewById(R.id.list);
mliListView.setEmptyView(findViewById(R.id.empty));
}
}
測(cè)試效果

注意
經(jīng)過(guò)本人測(cè)試,如果ListView包含在某些下拉刷新框架中,這樣做是沒(méi)有效果的,應(yīng)該是沖突了。
4、android:duplicateParentState="true"
該屬性可以讓子View跟隨其Parent的狀態(tài)。常見(jiàn)的使用場(chǎng)景是某個(gè)按鈕特別小,為了設(shè)置點(diǎn)擊事件,給其包裹一層Parent布局,將點(diǎn)擊事件寫到Parent上,如果希望被包裹按鈕的點(diǎn)擊效果對(duì)應(yīng)的Selector繼續(xù)生效的話,就可以使用它了,來(lái)個(gè)有說(shuō)服力的測(cè)試案例。
準(zhǔn)備好4個(gè)圖片,做成2個(gè) StateListDrawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 觸摸模式下單擊時(shí)的背景圖片-->
<item android:drawable="@drawable/backp" android:state_pressed="true" />
<!-- 默認(rèn)時(shí)的背景圖片-->
<item android:drawable="@drawable/bacn" />
</selector>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/press" android:state_pressed="true" />
<item android:drawable="@drawable/nomal" />
</selector>
布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:id="@+id/content"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_centerInParent="true"
android:background="@drawable/selector_back">
<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="@drawable/selector"
android:duplicateParentState="true" />
</RelativeLayout>
</RelativeLayout>
此時(shí)直接運(yùn)行測(cè)試,只有按鈕有點(diǎn)擊事件,直接點(diǎn)擊按鈕

給RelativeLayout添加點(diǎn)擊事件
RelativeLayout rl = (RelativeLayout) findViewById(R.id.content);
rl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
此時(shí)運(yùn)行測(cè)試,按鈕和相對(duì)布局都有點(diǎn)擊事件,點(diǎn)擊相對(duì)布局,發(fā)現(xiàn)按鈕并沒(méi)有變化

給Button添加 android:duplicateParentState="true
<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="@drawable/selector"
android:duplicateParentState="true" />
再次運(yùn)行測(cè)試,再次點(diǎn)擊相對(duì)布局,發(fā)現(xiàn)按鈕也跟著變化了
