Android自定義對話框提供用戶輸入選項
最近在寫一個關於服務器站點性能量化評分的小功能,主要是根據Apdex公式來進行評判的,需要提供一個讓用戶自己設置最優響應時間門檻的值的選項。由於頁面空間的限制,採取了通過彈出自定義對話框的形式來實現這一功能,具體實現如下:
Apdex詳解http://blog.csdn.net/sunny_sailor/article/details/51240313
- 自定義佈局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/compare_edit_last_set"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:text="@string/compare_edit_default"/>
</LinearLayout>
<LinearLayout
android:layout_marginLeft="4dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/compare_edit_time"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:hint="@string/compare_edit_set"
android:imeOptions="actionDone"
android:maxHeight="48dp"
android:minLines="1"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_alignBaseline="@id/compare_edit_time"
android:layout_marginRight="16dp"
android:layout_weight="1"
android:maxHeight="48dp"
android:text="@string/compare_edit_set_unit">
</TextView>
</LinearLayout>
</LinearLayout>
- 代碼文件
/**
* 提供設置量化評分的T值對話框
*/
private void settingDialog(final Context context)
{
final SharedPreferences.Editor editor = mPreferences.edit();
LayoutInflater layoutInflater = LayoutInflater.from(context);
//獲取自定義的視圖
View editView = layoutInflater.inflate(R.layout.dialog_compare_edit,null);
//省略部分代碼
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(getString(R.string.compare_edit_title));
//設置視圖
builder.setView(editView);
//獲取輸入的值
final EditText eTime = (EditText)editView.findViewById(R.id.compare_edit_time);
builder.setPositiveButton(getString(R.string.button_ok), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(("").equals(eTime.getText().toString()))
{
Toast.makeText(context,getString(R.string.input_reminder),Toast.LENGTH_SHORT)
.show();
changeShowing(false,dialog);
}
else{
int time = Integer.valueOf(eTime.getText().toString());
if(time <1 || time > 100000)
{
Toast.makeText(context,getString(R.string.input_reminder),Toast.LENGTH_SHORT)
.show();
changeShowing(false,dialog);
}else{
//還原PositiveButton的關閉效果
changeShowing(true,dialog);
}
}
}
}).setNegativeButton(getString(R.string.button_cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//還原NegativeButton的關閉效果
changeShowing(true,dialog);
}
}).create().show();
}
/**
* mShowing是Dialog類的一個成員變量,在setPositiveButton中改變之後會影響setNegativeButton,
* 如點擊“取消”按鈕將不能關閉對話框,解決辦法在“取消”的onClick事件中再改變mShowing的值
* @param isShow
* @param dialog
*/
private void changeShowing(boolean isShow,DialogInterface dialog){
//通過反射機制得到變量並改變變量的值
try {
Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
field.setAccessible(true);
field.set(dialog, isShow);
} catch (Exception e) {
e.printStackTrace();
}
}
上面代碼中的changeShowing方法僅僅適用於android.app.AlertDialog,如果使用的是v7包下的AlertDialog,則可以使用如下代碼代替來達到輸入數據不正確或爲空時,點擊按鈕對話框不關閉的效果。
/**
* 提供設置量化評分的T值對話框
*/
private void settingDialog(final Context context)
{
LayoutInflater layoutInflater = LayoutInflater.from(context);
View editView = layoutInflater.inflate(R.layout.dialog_compare_edit,null);
TextView lastSetTimeText = (TextView)editView.findViewById(R.id.compare_edit_last_set);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(getString(R.string.compare_edit_title));
builder.setView(editView);
//獲取輸入的值
final EditText eTime = (EditText)editView.findViewById(R.id.compare_edit_time);
builder.setPositiveButton(getString(R.string.button_ok),null);//只設置文本,而不添加監聽事件
builder.setNegativeButton(getString(R.string.button_cancel),null);//只設置文本,而不添加監聽事件
final AlertDialog alertDialog = builder.create();
alertDialog.show();
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(("").equals(eTime.getText().toString()))
{
Toast.makeText(context,getString(R.string.input_reminder),Toast.LENGTH_SHORT)
.show();
}
else{
//do something
//關閉對話框
alertDialog.dismiss();
}
}
}
});
}