【发布时间】:2014-08-29 07:46:27
【问题描述】:
我是 android 编程新手,想扩展一个按钮类,以便可以向按钮添加自定义字段,然后根据字段更改按钮的显示方式。我已经完成了一些教程,有一个很好的 bash 并且有一些可以编译但不起作用的东西 - 所以我正在寻找关于我哪里出错的提示。
我想要实现的目标比我的示例更复杂,但是一旦我可以让这个示例运行,我想我会很好地完成其余的工作。
对于我的示例,我想要一个具有 isRed 布尔成员/属性的按钮。如果设置了成员,则我们将按钮上的可绘制矩形更改为红色,如果为假,则可绘制为绿色。
为此,我有:
- 在我的 res/drawable 目录中创建了一个 redbutton.xml 文件。此文件包含一个选择器,该选择器提供红色或绿色矩形,具体取决于 isRed 状态。
- 在我的 src/com.example.redbutton 目录中创建了一个 RedButton.java 文件。此文件包含一个扩展 Button 的 RedButton 类
- 在我的 res/values 目录中创建了一个 attrs.xml 文件。这列出了 isRed 成员/属性
- 在我的主布局中放置两个按钮。一个按钮将 custom:isRed 设置为 true,另一个按钮将 isRed 设置为 false。
理论上,我的应用程序中应该有两个按钮,一个红色,一个绿色。不幸的是,我有两个绿色按钮!我似乎无法获得
<item custom:isRed="true">
在 redbutton.xlm 中评估为真。但是,如果我将此行更改为
<item android:state_pressed="true">
然后,每当我按下按钮时,我都会有一个红色按钮。对我来说,问题似乎是访问我的自定义字段总是评估为假!
下面是我写的代码。谁能给我一些关于我做错了什么的提示?
redbutton.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.example.redbutton" >
<!-- if the isred value is true, then make the background red -->
<!-- <item android:state_pressed="true"> -->
<item custom:isred="true" >
<shape android:shape="rectangle" >
<size android:width="100dp"
android:height="100dp"/>
<solid android:color="#ff0000"/>
</shape>
</item>
<!-- otherwise make it green -->
<item >
<shape android:shape="rectangle" >
<size android:width="100dp"
android:height="100dp"/>
<solid android:color="#00ff00"/>
</shape>
</item>
</selector>
RedButton.java
package com.example.redbutton;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.Button;
public class RedButton extends Button
{
private boolean mIsRed;
public RedButton (Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.RedButton,
0, 0);
try {
mIsRed = a.getBoolean(R.styleable.RedButton_isred, false);
} finally {
a.recycle();
}
}
//sets and gets for members
public boolean getIsRed()
{
return mIsRed;
}
public void setIsRed(boolean isRed)
{
mIsRed = isRed;
}
}
attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="RedButton">
<attr name="isred" format="boolean" />
</declare-styleable>
</resources>
fragment_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.example.redbutton"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.redbutton.MainActivity$PlaceholderFragment" >
<com.example.redbutton.RedButton
android:id="@+id/Button1"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="RedButton"
android:textSize="30sp"
custom:isred="true"
android:drawableBottom="@drawable/redbutton"
/>
<com.example.redbutton.RedButton
android:id="@+id/Button2"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="GreenButton"
android:textSize="30sp"
custom:isred="false"
android:drawableBottom="@drawable/redbutton"
/>
</LinearLayout>
【问题讨论】:
标签: java android button android-custom-view extends