描述
这是个简单的需求,原生的文件管理器files(documents)新建文件夹时弹出的dialog,在不输入任何字符的情况下,也可以点击确定键,并生成一个名为(invalid)文件夹。
预期需要改为在editText为空时,确定键不可点击。 对比看下flyme,在点击新建文件夹后,editText会自动填充“未命名文件夹”,删除字符后,确定键不可点击。也是个不错的设计。修改
修改点比较简单,直接上代码。代码位置 aosp\packages\apps\DocumentsUI\src\com\android\documentsui\CreateDirectoryFragment.java
以下就是创建dialog的方法,看到重写了onCreateDialog便于复用。方法中设置了dialog标题、文本框、确定键、取消键。
就是首图左边的样式。
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Context context = getActivity();
final ContentResolver resolver = context.getContentResolver();
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
final View view = dialogInflater.inflate(R.layout.dialog_file_name, null, false);
final EditText editText = (EditText) view.findViewById(android.R.id.text1);
builder.setTitle(R.string.menu_create_dir);
builder.setView(view);
builder.setPositiveButton(
android.R.string.ok,
new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
createDirectory(editText.getText().toString());
}
});
builder.setNegativeButton(android.R.string.cancel, null);
final AlertDialog dialog = builder.create();
// Workaround for the problem - virtual keyboard doesn t show on the phone.
Shared.ensureKeyboardPresent(context, dialog);
//这里加(1)
editText.setOnEditorActionListener(
new OnEditorActionListener() {
@Override
public boolean onEditorAction( //这里是对键盘的 enter键做处理
TextView view, int actionId, @Nullable KeyEvent event) {
if ((actionId == EditorInfo.IME_ACTION_DONE) || (event != null
&& event.getKeyCode() == KeyEvent.KEYCODE_ENTER
&& event.hasNoModifiers())) {
createDirectory(editText.getText().toString());
dialog.dismiss();
return true;
}
return false;
}
});
return dialog;
}
复制代码
在上面代码(1)处,即在builder.create()之后加入以下代码
dialog.setOnShowListener(new DialogInterface.OnShowListener() { //*1
@Override
public void onShow(DialogInterface dialog) {
final Button btn = ((AlertDialog) dialog).getButton(DialogInterface.BUTTON_POSITIVE); //*2
btn.setEnabled(false); //*3
editText.addTextChangedListener(new TextWatcher() { //*4
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
@Override
public void afterTextChanged(Editable s) { //*5
String name = s.toString().trim(); //*6
if (name.isEmpty()) {
btn.setEnabled(false);
} else {
btn.setEnabled(true);
}
}
});
}
});
复制代码
*1: 由于在onCreateDialog()方法中只是创建了dialog, final AlertDialog dialog = builder.create();
,dialog并没有.show(),所以dialog还没有绑定view,如果直接写 *2,会爆出空指针!!!而调用dialog.setOnShowListener()的onShow(),则是在dialog.show()之后才会回调,避免的空指针问题, 而且更好的是,你不必关心dialog会在何时何处创建,都可以对其创建后进行操作。
*2:一定需要在dialog.show()之后执行!否则必定会出现空指针。
*3:在dialog创建时就设置确定键为false,否则会有一个很尴尬的问题,弹出dialog后,确定键是可以点击的,但输入字符再删除后,确定键就不能点击。
*4:对editText输入状态做监听
*5:当输入字符后,判断editText内容是否为空
*6:将输入内容转为string并去除空格
到这里应该接结束了。如果自定义dialog,对于输入的字符还是需要做更复杂的判断处理。像这里的文件夹命名,是不允许为“.”等一些特殊命名。在实际开发中要注意判断。
修改后的效果