给一个辅助类 ShortcutEditorDialog.java
/*
* Copyright (C) 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.anycut;
import Android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent.ShortcutIconResource;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
/**
* A dialog that can edit a shortcut intent. For now the icon is displayed, and
* only the name may be edited.
*/
public class ShortcutEditorDialog extends AlertDialog implements OnClickListener, TextWatcher {
static final String STATE_INTENT = "intent";
private boolean mCreated = false;
private Intent mIntent;
private ImageView mIconView;
private EditText mNameView;
private OnClickListener mOnClick;
private OnCancelListener mOnCancel;
public ShortcutEditorDialog(Context context, OnClickListener onClick, OnCancelListener onCancel) {
super(context);
mOnClick = onClick;
mOnCancel = onCancel; //取消
// Setup the dialog
View view = getLayoutInflater().inflate(R.layout.shortcut_editor, null, false); //记载UI界面
setTitle(R.string.shortcutEditorTitle);//设置标题
setButton(context.getText(android.R.string.ok), this);//设置确定事件
setButton2(context.getText(android.R.string.cancel), mOnClick);//设置取消事件
setOnCancelListener(mOnCancel);//设置取消事件
setCancelable(true);//这个对话框是设置是否与返回键可取消
setView(view); //设置UI试图
mIconView = (ImageView) view.findViewById(R.id.shortcutIcon); //快捷方式图标位置
mNameView = (EditText) view.findViewById(R.id.shortcutName); //快捷方式别名输入框
}
public void onClick(DialogInterface dialog, int which) {
if (which == BUTTON1) { //如果是确定按钮
String name = mNameView.getText().toString(); //获取快捷方式别名
if (TextUtils.isEmpty(name)) { //如果为空
// Don't allow an empty name
mNameView.setError(getContext().getText(R.string.errorEmptyName));//显示输入框为空的告警
return;//消失
}
mIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);//设置快捷方式的别名
}
mOnClick.onClick(dialog, which);
}
@Override
protected void onCreate(Bundle state) {
super.onCreate(state);
if (state != null) {
mIntent = state.getParcelable(STATE_INTENT);
}
mCreated = true;
// If an intent is set make sure to load it now that it's safe
if (mIntent != null) {
loadIntent(mIntent);
}
}
@Override
public Bundle onSaveInstanceState() {
Bundle state = super.onSaveInstanceState();
state.putParcelable(STATE_INTENT, getIntent());
return state;
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// Do nothing
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Do nothing
}
public void afterTextChanged(Editable text) {
if (text.length() == 0) {
mNameView.setError(getContext().getText(R.string.errorEmptyName));//如果快捷方式的名称为空设置提示
} else {
mNameView.setError(null);
}
}
/**
* Saves the current state of the editor into the intent and returns it.
*
* @return the intent for the shortcut being edited
*/
public Intent getIntent() {
mIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, mNameView.getText().toString());
return mIntent;
}
/**
* Reads the state of the shortcut from the intent and sets up the editor
*
* @param intent
* A shortcut intent to edit
*/
public void setIntent(Intent intent) {
mIntent = intent;
if (mCreated) {
loadIntent(intent);
}
}
/**
* Loads the editor state from a shortcut intent.
*
* @param intent
* The shortcut intent to load the editor from
*/
private void loadIntent(Intent intent) {
// Show the icon
Bitmap iconBitmap = intent.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON); //获取快捷方式的图标
if (iconBitmap != null) { //如果不为空
mIconView.setImageBitmap(iconBitmap); //添加到试图
} else {
ShortcutIconResource iconRes = intent.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
if (iconRes != null) {
int res = getContext().getResources().getIdentifier(iconRes.resourceName, null, iconRes.packageName);//默认加载iconRes的地址
mIconView.setImageResource(res);
} else {
mIconView.setVisibility(View.INVISIBLE); //不要显示
}
}
// Fill in the name field for editing
mNameView.addTextChangedListener(this); //监听字符的变化
mNameView.setText(intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME));//初始化数据
// Ensure the intent has the proper flags
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //设置这个将是最顶层的应用 其他的将被关闭(模式)
}
}