【问题标题】:How to apply marquee to the Button programmatically in Android?如何在Android中以编程方式将选取框应用于按钮?
【发布时间】:2015-04-10 05:02:00
【问题描述】:

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

    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);

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

【问题讨论】:

    标签: android button textview android-button


    【解决方案1】:

    请试试这个..

    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();
    

    【讨论】:

      猜你喜欢
      • 2020-03-14
      • 1970-01-01
      • 1970-01-01
      • 2021-12-18
      • 1970-01-01
      • 2011-11-15
      • 1970-01-01
      • 2014-03-06
      • 1970-01-01
      相关资源
      最近更新 更多