亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

初始化設置的內容視圖后,我可以更改 Android Button 的樣式嗎?

初始化設置的內容視圖后,我可以更改 Android Button 的樣式嗎?

LEATH 2021-11-24 18:51:17
我想根據字符串值在 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);                    }                }            }        } 
查看完整描述

2 回答

?
收到一只叮咚

TA貢獻1821條經驗 獲得超5個贊

setTextAppearance()您使用的這種形式已被棄用。將您的代碼更改為以下內容:


if (Build.VERSION.SDK_INT < 23) {

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

} else {

    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


查看完整回答
反對 回復 2021-11-24
?
jeck貓

TA貢獻1909條經驗 獲得超7個贊

   <?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/shape_button_gray" android:state_enabled="false" />

    <item android:drawable="@drawable/shape_button_colored" android:state_enabled="true" />

</selector>

現在將此選擇器添加到按鈕作為背景


       <Button

        android:id = "@+id/button"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:background="@drawable/selector_button"

        android:textColor="@android:color/white" />

現在在代碼中


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

 dynakey[ID].setEnabled(true)

} else {

dynakey[ID].setEnabled(false)

}


查看完整回答
反對 回復 2021-11-24
  • 2 回答
  • 0 關注
  • 149 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號