开发中要使用到复杂一点的对话框,里面可以编辑内容。
先看效果:
实现方法有两种,一是自定一个继承类:
PLMNDialog.java:
package com.android.plmn;
import com.android.plmn.PLMNItemContentList.PlmnItem;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class PLMNDialog extends AlertDialog implements TextWatcher,
RadioGroup.OnCheckedChangeListener
{
static final int BTN_OK = DialogInterface.BUTTON_POSITIVE;
static final int BTN_CANCEL = DialogInterface.BUTTON_NEGATIVE;
static final int CREATE_MODE = 0;
static final int UPDATE_MODE = 1;
private final Context mContext;
private final DialogInterface.OnClickListener mListener;
private final int mode;
private final PlmnItem item;
private View mView;
private EditText mIdEditText;
private RadioGroup mRadioGroup;
private String mId;
private int mType = 0;
public String getPLMNId(){
if(mId != null){
return mId;
} else {
return "";
}
}
public int getPLMNType(){
return mType;
}
public int getPLMNMode(){
return mode;
}
public int getPLMNItemIndex(){
if(item != null){
return item.getIndex();
}
return -1;
}
public PLMNDialog(Context context, DialogInterface.OnClickListener listener, PlmnItem item, int mode)
{
super(context);
this.mContext = context;
this.mListener = listener;
this.item = item;
this.mode = mode;
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
if(mode == CREATE_MODE){
setTitle(R.string.dialog_plmn_create_item);
} else if(mode == UPDATE_MODE){
setTitle(R.string.dialog_plmn_update_item);
}
final Context dialogContext = new ContextThemeWrapper(mContext,
android.R.style.Theme_Light);
final LayoutInflater dialogInflater = (LayoutInflater) dialogContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mView = dialogInflater.inflate(R.layout.edit_plmn_item, null);
if(mView != null){
setView(mView);
}
setInverseBackgroundForced(true);
mIdEditText = (EditText) mView.findViewById(R.id.edit_plmn_item_id);
mRadioGroup = (RadioGroup) mView.findViewById(R.id.radio_network_type_group);
mIdEditText.addTextChangedListener(this);
mIdEditText.setText(item.getId());
mRadioGroup.setOnCheckedChangeListener(this);
setRadioCheck(item.getType());
setButton(BTN_OK, mContext.getString(android.R.string.ok), mListener);
setButton(BTN_CANCEL, mContext.getString(android.R.string.cancel), mListener);
super.onCreate(savedInstanceState);
validate();
}
private void setRadioCheck(int index){
RadioButton btn = (RadioButton)mRadioGroup.getChildAt(index);
if(btn != null){
btn.setChecked(true);
}
}
public void onTextChanged(CharSequence s, int start, int before, int count){
}
public void beforeTextChanged(CharSequence s, int start, int count,int after){
}
public void afterTextChanged(Editable editable){
validate();
}
private void validate()
{
mId = mIdEditText.getText().toString().trim();
if(getButton(BTN_OK) != null)
{
boolean valid = true;
if(item!=null && item.getId().equals(mId) && item.getType() == mType ){
valid = false;
}
if(!TextUtils.isEmpty(mId) && valid){
getButton(BTN_OK).setEnabled(true);
} else{
getButton(BTN_OK).setEnabled(false);
}
}
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
switch (checkedId) {
case R.id.radio0:
mType = 0;
break;
case R.id.radio1:
mType = 1;
break;
case R.id.radio2:
mType = 2;
break;
default:
break;
}
validate();
}
}
调用方法:
private PLMNDialog mDialog;
}
第二种方法:
private void displayEditItemDialog(PlmnItem item) {
// Wrap our context to inflate list items using correct theme
final Context dialogContext = new ContextThemeWrapper(this,
android.R.style.Theme_Light);
final LayoutInflater dialogInflater = (LayoutInflater) dialogContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = dialogInflater.inflate(R.layout.edit_plmn_item, null);
EditText mIdEditText = (EditText) view.findViewById(R.id.edit_plmn_item_id);
mIdEditText.setInputType(InputType.TYPE_CLASS_PHONE);
mIdEditText.setOnClickListener(mEditListener);
mIdEditText.addTextChangedListener(mTextWatcher);
RadioGroup mRadioGroup = (RadioGroup)view.findViewById(R.id.radio_network_type_group);
mIdEditText.setText(item.plmn_id);
mRadioGroup.setOnCheckedChangeListener(mRadioListener);
final DialogInterface.OnClickListener clickListener = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Log.e("HJJ", "mTempItemId:" + mTempItemId + ",mTempItemType:" + mTempItemType);
}
};
new AlertDialog.Builder(this).setTitle("创建")
.setView(view)
.setNegativeButton(android.R.string.cancel, null)
.setPositiveButton(android.R.string.ok, clickListener)
.create()
.show();
}
edit_plmn_item.xml
资源文件;
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="300sp"
android:layout_height="wrap_content">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="@string/plmn_item_id_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<EditText
android:layout_height="wrap_content"
android:id="@+id/edit_plmn_item_id"
android:layout_margin="2dip"
android:singleLine="true"
android:inputType="number"
android:scrollHorizontally="true"
android:layout_width="match_parent">
<requestFocus></requestFocus>
</EditText>
<TextView
android:text="@string/plmn_item_support_tech"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<RadioGroup
android:id="@+id/radio_network_type_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:layout_height="wrap_content"
android:id="@+id/radio0"
android:text="@string/plmn_network_type_gsm"
android:layout_width="wrap_content">
</RadioButton>
<RadioButton
android:layout_height="wrap_content"
android:id="@+id/radio1"
android:text="@string/plmn_network_type_gsm_compact"
android:layout_width="wrap_content">
</RadioButton>
<RadioButton
android:layout_height="wrap_content"
android:id="@+id/radio2"
android:text="@string/plmn_network_type_td"
android:layout_width="wrap_content">
</RadioButton>
</RadioGroup>
</LinearLayout>
</ScrollView>