具有可修改的单行标题的Android Dialog

时间:2011-06-12 19:37:13

标签: android dialog title

简而言之,我正在开发一个“DurationPickerDialog”,其工作方式类似于DatePickerDialog的工作方式,但基于xsd:duration类型工作,因此用户指定年,月,日等的数量。

我还实现了一个“模糊持续时间”功能,它将持续时间视为“一个月前”之类的字符串。我真的希望能够以与DatePickerDialog标题更新相同的方式更新DurationPickerDialog的标题,但似乎存在问题。在DatePickerDialog中,它们始终将它设置为单行,因此它不会“跳跃”。以下是Android的源代码如何处理DatePickerDialog的标题。

// Note: before the skim-readers look at this bit, realize that this is NOT my
// code but Android's internal code for the DatePickerDialog.
@Override
public void show() {
    super.show();

    /* Sometimes the full month is displayed causing the title
     * to be very long, in those cases ensure it doesn't wrap to
     * 2 lines (as that looks jumpy) and ensure we ellipsize the end.
     */
    TextView title = (TextView) findViewById(R.id.alertTitle);
    title.setSingleLine();
    title.setEllipsize(TruncateAt.END);
}

很遗憾,我无法访问他们的R.id.alertTitle,因为它是com.android.internal.R的一部分。

我见过类似this stackoverflow post的实现,它会让我修改Window.FEATURE_CUSTOM_TITLE属性,但这似乎不允许我在此之后轻松修改标题。

还有another stackoverflow post提到了如何在两个不同的XML布局之间在运行时更改标题,但这似乎也没有太大帮助,因为标题应该每次都修改持续时间的变化,以及为每个持续时间创建XML布局显然不是一个好主意。

所以,既然他们通过访问我们凡人无法访问的价值来“欺骗”,那么还有另外一种方法可以实现吗?


编辑:并且通过一些黑魔法,它现在似乎 像我想要的那样删除文本?只是早些时候它没有,现在我似乎无法重现这个问题。所以,我想,当我们在这里时,有人可以向我解释我是如何完成这种魔法的吗?

2 个答案:

答案 0 :(得分:0)

我将通过扩展Dialog类并为其创建自定义xml布局来实现自定义对话框 您需要一些自定义按钮背景用于加/减和顶部/底部组合以及一些按钮侦听器来操纵值。
由于您需要持续时间值,因此您可能需要比现有对话框更多的空间 这样您就可以将标题设置为您喜欢的任何内容。如果您需要代码示例,请告诉我。

实施例: 对话框类:

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class DurationDialog extends Dialog {

    private Button yearButtonPlus;
    private Button yearButtonMinus;
    private TextView dialogBody;
    private TextView dialogTitle;

    private String dialogBodyString;
    private String dialogTitleString;


    public DurationDialog(final Context context, String dialogBody, String dialogTitle) {
        super(context,R.style.CustomDialogTheme);
        this.dialogBodyString = dialogBody;
        this.dialogTitleString = dialogTitle;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.setContentView(R.layout.my_dialog);

        yearButtonPlus = (Button) findViewById(R.id.dialog_year_button_plus);
        yearButtonMinus = (Button) findViewById(R.id.dialog_year_button_minus);
        dialogBody = (TextView) findViewById(R.id.dialog_body);
        dialogTitle = (TextView) findViewById(R.id.dialog_title);

        dialogBody.setText(dialogBodyString);
        dialogTitle.setText(dialogTitleString);


        yearButtonPlus.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //do year increment here
            }
        });

        //etc...
    }

}

在您的活动中,您调用this.showDialog(DURATION_DIALOG); // DURATION_DIALOG只是在Activity顶部指定的一个整数,用于标识下一个代码的对话框,该代码处理实际创建对话框:

import android.app.Activity;
import android.app.Dialog;

public class MyActivity extends Activity  {

    private Dialog dialog;


    //Lots of other activity stuff...



    protected Dialog onCreateDialog(int id) {

        switch (id) {
            case DURATION_DIALOG:
                dialog = new DurationDialog(Activity.this, "your title", "your body");
                dialog.setOnDismissListener(onDismissListener);
                break;
            default:
                dialog = null;
        }
        return dialog;
    }

}

//put this listener in as an inner class of MyActivity:
    private DialogInterface.OnDismissListener onDismissListener = new DialogInterface.OnDismissListener() {
    @Override
    public void onDismiss(DialogInterface dialog) {

        DurationDialog dialog = (DurationDialog) dialog;
        //grab the duration stuff out of your dialog and do stuff with it....

    }
};

最后,您可以在styles.xml文件中设置对话框主题。例如:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="CustomDialogTheme" parent="@android:style/Theme.Dialog">
        <item name="android:windowBackground">@drawable/bgnd_transparent</item>
        <item name="android:windowIsFloating">true</item>
    </style>
</resources>
祝你好运!

答案 1 :(得分:0)

如果你想在不使用自定义视图的情况下修改对话框标题的TextView(更改文本,样式,行为......),就这样做:

TextView tv = (TextView) dialog.findViewById(android.R.id.title);
tv.setText("New title");
...