Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.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
Java 带有图像和文本的Android选择器_Java_Android_Button_Imagebutton_Android Xml - Fatal编程技术网

Java 带有图像和文本的Android选择器

Java 带有图像和文本的Android选择器,java,android,button,imagebutton,android-xml,Java,Android,Button,Imagebutton,Android Xml,我有一个按钮,我给一个颜色,图像和文字如下: android:background="@color/green" android:drawableLeft="@drawable/custom_routes_start_button_icon" android:text="@string/custom_route_start" android:background="@color/red" android:drawableLeft="@drawable/custom_routes_stop_bu

我有一个按钮,我给一个颜色,图像和文字如下:

android:background="@color/green"
android:drawableLeft="@drawable/custom_routes_start_button_icon"
android:text="@string/custom_route_start"
android:background="@color/red"
android:drawableLeft="@drawable/custom_routes_stop_button_icon"
android:text="@string/custom_route_stop"
这是未选择的状态,希望选择的状态如下:

android:background="@color/green"
android:drawableLeft="@drawable/custom_routes_start_button_icon"
android:text="@string/custom_route_start"
android:background="@color/red"
android:drawableLeft="@drawable/custom_routes_stop_button_icon"
android:text="@string/custom_route_stop"

据我所知,在选择器中为一个项目指定文本或drawableLeft(仅drawable)是不可能的。有人知道实现这一目标的好方法吗?也许te选择器也可以引用另一个xml文件?

您应该使用两个按钮,并且只显示其中一个按钮。使用XML中的
android:visibility
setVisibility()
显示/隐藏按钮


因此,首先要使开始按钮可见,并隐藏停止按钮。当用户按下开始按钮时,隐藏开始按钮并显示停止按钮。当用户按下“停止”按钮时,将其隐藏并再次显示“开始”按钮。

您的意思是,如果单击按钮,是否要更改按钮的背景色? 是的,你能做到

首先界定国家

private int btnState = 1;
private final static int BUTTON_STATE_SELECTED = 0;
private final static int BUTTON_STATE_UNSELECTED = 1;
private View.OnClickListener mOnClickBtnRoute = new View.OnClickListener() {
switch(btnState) {
case BUTTON_STATE_SELECTED:
btnRoute.setBackgroundColor(green);
btnRoute.setText(start);
Drawable img = getContext().getResources().getDrawable( R.drawable.custom_routes_start_button_icon );
btnRoute.setCompoundDrawablesWithIntrinsicBounds( img, null, null, null );
btnState = BUTTON_STATE_UNSELECTED;
break;
case BUTTON_STATE_UNSELECTED:
btnRoute.setBackgroundColor(red);
btnRoute.setText(stop);
Drawable img = getContext().getResources().getDrawable( R.drawable.custom_routes_stop_button_icon );
btnRoute.setCompoundDrawablesWithIntrinsicBounds( img, null, null, null );
btnState = BUTTON_STATE_SELECTED;
break;
}
};
然后将id设置为您的按钮

android:id="@+id/btnRoute"
android:background="@color/green"
android:drawableLeft="@drawable/custom_routes_start_button_icon"
android:text="@string/custom_route_start"
在活动中声明按钮

Button btnRoute = (Button) findviewbyid(R.id.btnRoute);
之后,创建一个onclick监听器,它将根据状态更改按钮的颜色

private int btnState = 1;
private final static int BUTTON_STATE_SELECTED = 0;
private final static int BUTTON_STATE_UNSELECTED = 1;
private View.OnClickListener mOnClickBtnRoute = new View.OnClickListener() {
switch(btnState) {
case BUTTON_STATE_SELECTED:
btnRoute.setBackgroundColor(green);
btnRoute.setText(start);
Drawable img = getContext().getResources().getDrawable( R.drawable.custom_routes_start_button_icon );
btnRoute.setCompoundDrawablesWithIntrinsicBounds( img, null, null, null );
btnState = BUTTON_STATE_UNSELECTED;
break;
case BUTTON_STATE_UNSELECTED:
btnRoute.setBackgroundColor(red);
btnRoute.setText(stop);
Drawable img = getContext().getResources().getDrawable( R.drawable.custom_routes_stop_button_icon );
btnRoute.setCompoundDrawablesWithIntrinsicBounds( img, null, null, null );
btnState = BUTTON_STATE_SELECTED;
break;
}
};
然后别忘了将侦听器设置为按钮

btnRoute.setOnClickListener(mOnClickBtnRoute);

请记住所有的代码都是在这里编码的,所以可能会出现错误,所以请不要只是复制粘贴,而是尝试理解这个概念:)如果你对我的答案有什么问题,请随时在评论中提问

您可以通过以下代码进行更改:

在xml文件中编写以下代码:

 android:background="@color/green"
 android:drawableLeft="@drawable/custom_routes_start_button_icon"
 android:text="@string/custom_route_start" 
和按钮单击事件:

    yourButton = (TextView) findViewById(R.id.yourButton);

    yourButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Drawable checkImg = getApplicationContext().getResources().getDrawable(
                       R.drawable.custom_routes_stop_button_icon);
            yourButton.setCompoundDrawablesWithIntrinsicBounds(checkImg, null, null,
            null);
            yourButton.setBackgroundColor(red);
            yourButton.setText(custom_route_stop);
        }
    });
您还可以将此代码放在以下侦听器上:

 yourButton.setOnTouchListener(new OnTouchListener() {
        boolean isTouch = false;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                Drawable checkImg = getApplicationContext().getResources().getDrawable(
                       R.drawable.custom_routes_stop_button_icon);
                yourButton.setCompoundDrawablesWithIntrinsicBounds(checkImg, null, null, null);
                yourButton.setBackgroundColor(red);
                yourButton.setText(custom_route_stop);  
            }
            else {
                Drawable checkImg = getApplicationContext().getResources().getDrawable(
                       R.drawable.custom_routes_start_button_icon);
                yourButton.setCompoundDrawablesWithIntrinsicBounds(checkImg, null, null, null);
                yourButton.setBackgroundColor(green);
                yourButton.setText(custom_route_start);
            }
            return false;
        }
    });

我不确定ToggleButton(甚至复选框)是否适合您的用例。您确实可以为您的可绘制文件(左、右、上、下)使用选择器,使用带有选择器的背景,甚至为您的两个状态使用不同的文本

实际上,您可以为您的背景定义一个选择器(/res/color/custom_color.xml)

您可能必须使用样式才能使其显示为您想要的样式


虽然这是一个迟来的答案,但希望有人会觉得它有用。

尝试制作一个具有文本的按钮。背景和可绘制的左侧应该能够使用常规选择器(对于每个按钮)。文本可以通过使用onTouchListener(向下更改文本,向上更改文本)来完成。您所说的“选中”是什么意思?你是说按钮被点击了吗?或者它已获得焦点?选定的状态。因此,当用户单击它时,我也想更改处于选定状态的图像。所以未选择的是:绿色,开始图像,开始文本选择的是:红色,停止图像,停止文本是你可以!使用setcompounddrawable函数,请查看我编辑的答案!谢谢你的回答。这很容易理解,但从未想过这是解决此类问题的最佳方法。它是:-)StateSelector Drawable仅用于显示一个按钮的不同状态,例如按下、不活动等。但您需要的显然是两个按钮。在我的一个应用程序中,我有你想要的东西——一个交替显示的开始和停止按钮。所以我知道我在说什么;-)那么哪种解决方案在编程方面更好呢。更改代码中的可绘制、颜色和文本(如您的解决方案),或者两个按钮,一次只能看到一个?我已经实现了一个我用编程编写的按钮,但正如您所写的,您也可以使用两个按钮,并为两个按钮使用相同的侦听器。这是最好的选择。切换按钮解决我的问题