初始化设置的内容视图后,我可以更改 Android Button 的样式吗?

我想根据字符串值在 android 按钮视图中显示不同样式的按钮,我可以以编程方式执行此操作吗?


我可以以编程方式使用设置样式条件,即使我设置了 setTextAppearance 它没有反映在视图中吗?


  private Button dynakey[]= new Button[9];//8 buttons       

        //Parse the values of buttons 

        try {

            String sButtonString="";

            JSONArray jArrayButton = 

            ResponseUIObject.getJSONArray("BUTTON");

            for(int k=0;k<8;k++) {

                int ID=k+1;

                String dynakeyID = "dynakey"+(ID);

                int resID  = getResources().getIdentifier(dynakeyID, "id", getPackageName());

                dynakey[ID] =(Button) findViewById(resID);

                sButtonString= jArrayButton.getString(k-0);

                 //Set Enable or Disable logic here

                if ((ID% 2) == 0) {

                    // number is even its a right side button //Change style of button depending upon the value

                    if(sButtonString.trim().length() == 0) {

                        //If string is not null or not empty then Enable the button else not

                        dynakey[ID].setTextAppearance(this, R.style.RightDisableDynakeyButton);

                    }else{

                        dynakey[ID].setTextAppearance(this, R.style.RightEnableDynakeyButton);

                    }

                }

                else {

                    // number is odd its a left side button //Change style of button depending upon the value

                    if(sButtonString.trim().length() ==0) {

                        //If string is not null or empty then Enable the button else not

                        dynakey[ID].setTextAppearance(this, R.style.LeftDisableDynakeyButton);

                    }else{

                        dynakey[ID].setTextAppearance(this, R.style.LeftEnableDynakeyButton);

                    }

                }

            }

        } 


LEATH
浏览 121回答 2
2回答

收到一只叮咚

setTextAppearance()您使用的这种形式已被弃用。将您的代码更改为以下内容:if (Build.VERSION.SDK_INT < 23) {&nbsp; &nbsp; dynakey[ID].setTextAppearance(this, R.style.LeftDisableDynakeyButton);} else {&nbsp; &nbsp; dynakey[ID].setTextAppearance(R.style.LeftDisableDynakeyButton);}由于setTextAppearance(styleId)引入了API 23 。尝试一下。编辑您的代码:(1) 这一行:private Button dynakey[]= new Button[9];创建一个包含 9 个按钮的数组,而不是如注释所述的 8 个按钮。(2) 您在try块内应用更改(我看不到catch块),因此不会传播任何错误。会不会是按钮的id不正确?(3) 发布你的样式和 xml

jeck猫

&nbsp; &nbsp;<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">&nbsp; &nbsp; <item android:drawable="@drawable/shape_button_gray" android:state_enabled="false" />&nbsp; &nbsp; <item android:drawable="@drawable/shape_button_colored" android:state_enabled="true" /></selector>现在将此选择器添加到按钮作为背景&nbsp; &nbsp; &nbsp; &nbsp;<Button&nbsp; &nbsp; &nbsp; &nbsp; android:id = "@+id/button"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:background="@drawable/selector_button"&nbsp; &nbsp; &nbsp; &nbsp; android:textColor="@android:color/white" />现在在代码中if(sButtonString.trim().length() == 0) {&nbsp;dynakey[ID].setEnabled(true)} else {dynakey[ID].setEnabled(false)}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java