如何在Android中以编程方式将marquee应用于Button?

时间:2015-04-10 05:02:00

标签: android button textview android-button

我正在尝试将选取框添加到我的提醒按钮(否定按钮)文本中。这是代码:

    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
                Toast.makeText(getApplicationContext(), "Clicked Yes", Toast.LENGTH_LONG).show();
        }
    });

    builder.setNegativeButton("Dont showDont showDont showDont showDont showDont showDont showDont show",
            new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
        }
    });

    builder.setNeutralButton("Dont show", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            Toast.makeText(getApplicationContext(), "Clicked Neutral", Toast.LENGTH_LONG).show();
        }
    });

    AlertDialog alertBox = builder.create();

    alertBox.show();

        Button button = alertBox.getButton(AlertDialog.BUTTON_NEGATIVE);
        if(button==null){
             Toast.makeText(getApplicationContext(), "Button is NULL",Toast.LENGTH_LONG).show();
        } else {         
             Toast.makeText(getApplicationContext(), button.getText(), Toast.LENGTH_LONG).show();
             button.setEllipsize(TruncateAt.MARQUEE);
             button.setMaxLines(1);
             button.setSelected(true);
             button.setMarqueeRepeatLimit(10);
        }

我还尝试添加以下行:

button.setHorizontallyScrolling(true);

但它没有显示按钮本身的文字。请帮帮我。

1 个答案:

答案 0 :(得分:0)

试试这个......

builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
                Toast.makeText(getApplicationContext(), "Clicked Yes", Toast.LENGTH_LONG).show();
        }
    });

    builder.setNegativeButton("Dont showDont showDont showDont showDont showDont showDont showDont show",
            new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
        }
    });

    builder.setNeutralButton("Dont show", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            Toast.makeText(getApplicationContext(), "Clicked Neutral", Toast.LENGTH_LONG).show();
        }
    });

    final AlertDialog alertBox = builder.create();
    alertBox.setOnShowListener(new OnShowListener() {

        @Override
        public void onShow(DialogInterface dialog) {
            Button posBtn =alertBox.getButton(DialogInterface.BUTTON_POSITIVE);
            Button negBtn =alertBox.getButton(DialogInterface.BUTTON_NEGATIVE);
            Button neuBtn =alertBox.getButton(DialogInterface.BUTTON_NEUTRAL);


            LinearLayout.LayoutParams posParams = (LinearLayout.LayoutParams) posBtn.getLayoutParams();
            posParams.weight = 1;
            posParams.width = LayoutParams.MATCH_PARENT;

            LinearLayout.LayoutParams negParams = (LinearLayout.LayoutParams) negBtn.getLayoutParams();
            negParams.weight = 1;
            negParams.width = LayoutParams.MATCH_PARENT;

            LinearLayout.LayoutParams neuParams = (LinearLayout.LayoutParams) neuBtn.getLayoutParams();
            neuParams.weight = 1;
            neuParams.width = LayoutParams.MATCH_PARENT;

            posBtn.setLayoutParams(posParams);
            negBtn.setLayoutParams(negParams);
            neuBtn.setLayoutParams(neuParams);
        }
    });

    alertBox.show();