Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过android按钮设置后台可绘制状态_Android_Textview_Xml Drawable - Fatal编程技术网

通过android按钮设置后台可绘制状态

通过android按钮设置后台可绘制状态,android,textview,xml-drawable,Android,Textview,Xml Drawable,我正在建立一个多项选择问卷android程序。 我的onCreate()方法中有以下内容 这太令人困惑了! 我的实现出了什么问题?您可以在一个xml中设置形状,在另一个xml中设置按钮状态,然后尝试在这里使用xml以获得不同的状态 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> &l

我正在建立一个多项选择问卷android程序。 我的onCreate()方法中有以下内容

这太令人困惑了!
我的实现出了什么问题?

您可以在一个xml中设置形状,在另一个xml中设置按钮状态,然后尝试在这里使用xml以获得不同的状态

 <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:drawable="@drawable/save_button_active" android:state_focused="true" android:state_pressed="true"/>
    <item android:drawable="@drawable/save_button_active" android:state_focused="false" android:state_pressed="true"/>
    <item android:drawable="@drawable/save_button_active" android:state_focused="true"/>
    <item android:drawable="@drawable/save_button_inactive" android:state_focused="false" android:state_pressed="false"/>
    </selector>

由于setBackgroundDrawable(shape)方法已被弃用,我尝试了另一种方法,该方法实际上非常有效,并解决了问题:

((Button)btnArray[i]).setBackgroundResource(R.drawable.optionbutton);

似乎
setBackgroundDrawable()
已被弃用。您可以使用另一种替代方法,如
setBackgroundResource()

您是否尝试过此((按钮)btnArray[0])。setBackgroundDrawable(形状);是的,我试过了,同样的问题。我将编辑这个问题以消除混淆。用XML设置背景如何?还有
xmlns:android=”http://schemas.android.com/apk/res/android“
两次。但不确定这是否是个问题。我有几个XML,在运行时,我选择红色背景作为错误答案,绿色背景作为正确答案。感谢vmironov、DjackTorreborn和paul的帮助
((Button)btnArray[0]).setBackgroundDrawable(shape);
((Button)btnArray[1]).setBackgroundDrawable(shape);
((Button)btnArray[2]).setBackgroundDrawable(shape);
((Button)btnArray[4]).setBackgroundDrawable(shape);
((Button)btnArray[3]).setBackgroundDrawable(shape); //Button-3 only turns red
 <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:drawable="@drawable/save_button_active" android:state_focused="true" android:state_pressed="true"/>
    <item android:drawable="@drawable/save_button_active" android:state_focused="false" android:state_pressed="true"/>
    <item android:drawable="@drawable/save_button_active" android:state_focused="true"/>
    <item android:drawable="@drawable/save_button_inactive" android:state_focused="false" android:state_pressed="false"/>
    </selector>
((Button)btnArray[i]).setBackgroundResource(R.drawable.optionbutton);