I am working on Android Application. I want to have 4 buttons to be placed horizontally at the bottom of the screen. In these 4 buttons 2 buttons are having images on them. The border of the buttons should be black color and the border should be as thin as possible. When I click the button, I want the background of the button should be changed to blue color without the color of border to be changed and should be remained in that color for some time. How can I achieve this scenario in Android?
我正在开发Android应用程序。我想让4个按钮水平放置在屏幕的底部。在这4个按钮中,2个按钮上有图像。按钮的边框应为黑色,边框应尽可能薄。当我单击按钮时,我希望按钮的背景应该更改为蓝色而不更改边框的颜色,并且应该保留该颜色一段时间。如何在Android中实现此方案?
11 个解决方案
#1
114
One approach is to create an XML file like this in drawable
, called whatever.xml:
一种方法是在drawable中创建这样的XML文件,称为whatever.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="@drawable/bgalt" />
<item
android:state_focused="false"
android:state_pressed="true"
android:drawable="@drawable/bgalt" />
<item android:drawable="@drawable/bgnorm" />
</selector>
bgalt
and bgnorm
are PNG images in drawable.
bgalt和bgnormare PNG图像在drawable中。
If you create the buttons programatically in your activity, you can set the background with:
如果在活动中以编程方式创建按钮,则可以使用以下方法设置背景:
final Button b = new Button (MyClass.this);
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.whatever));
If you set your buttons' style with an XML, you would do something like:
如果您使用XML设置按钮的样式,您可以执行以下操作:
<Button
android:id="@+id/mybutton"
android:background="@drawable/watever" />
And finally a link to a tutorial. Hope this helps.
最后是教程的链接。希望这可以帮助。
#2
69
Save this code in drawable folder with "bg_button.xml" and call "@drawable/bg_button" as background of button in your xml:
使用“bg_button.xml”将此代码保存在drawable文件夹中,并将“@ drawable / bg_button”作为xml中按钮的背景调用:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<solid
android:color="#004F81" />
<stroke
android:width="1dp"
android:color="#222222" />
<corners
android:radius="7dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#89cbee"
android:endColor="#004F81"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#4aa5d4" />
<corners
android:radius="7dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
#3
8
Try This
尝试这个
final Button button = (Button) findViewById(R.id.button_id);
button.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP) {
button.setBackgroundColor(Color.RED);
} else if(event.getAction() == MotionEvent.ACTION_DOWN) {
button.setBackgroundColor(Color.BLUE);
}
return false;
}
});
#4
7
Refer this,
参考这个,
boolean check = false;
Button backward_img;
Button backward_img1;
backward_img = (Button) findViewById(R.id.bars_footer_backward);
backward_img1 = (Button) findViewById(R.id.bars_footer_backward1);
backward_img.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
check = true;
backward_img.setBackgroundColor(Color.BLUE);
}
});
if (check == true) {
backward_img1.setBackgroundColor(Color.RED);
backward_img.setBackgroundColor(Color.BLUE);
}
#5
5
If you want to change the backgorund image or color of the button when it is pressed, then just copy this code and paste in your project at exact location described below.
如果要在按下按钮时更改按钮的背景图像或颜色,则只需复制此代码并粘贴到项目中,如下所示。
<!-- Create new xml file like mybtn_layout.xml file in drawable -->
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/pressed" /> <!--pressed -->
<item android:drawable="@drawable/normal" /> <!-- Normal -->
</selector>
<!-- Now this file should be in a drawable folder and use this
single line code in button code to get all the properties of this xml file -->
<Button
android:id="@+id/street_btn"
android:layout_width="wrap_content"
android:background="@drawable/layout_a" > <!-- your required code -->
</Button>
#6
5
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- default -->
<item
android:state_pressed="false"
android:state_focused="false">
<shape
android:innerRadiusRatio="1"
android:shape="rectangle" >
<solid android:color="#00a3e2" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape>
</item>
<!-- button focused -->
<item
android:state_pressed="false"
android:state_focused="true">
<shape
android:innerRadiusRatio="1"
android:shape="rectangle" >
<solid android:color="#5a97f5" />
<padding
android:bottom="5dp"
android:left="10dp"
android:right="10dp"
android:top="5dp" />
</shape></item>
<!-- button pressed -->
<item
android:state_pressed="true"
android:state_focused="false">
<shape
android:innerRadiusRatio="1"
android:shape="rectangle" >
<solid android:color="#478df9"/>
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape></item>
</selector>
#7
4
Try this......
尝试这个......
First create an xml file named button_pressed.xml These are it's contents.
首先创建一个名为button_pressed.xml的xml文件。这些是它的内容。
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"
android:state_pressed="false"
android:drawable="@drawable/icon_1" />
<item android:state_focused="true"
android:state_pressed="true"
android:drawable="@drawable/icon_1_press" />
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="@drawable/icon_1_press" />
<item android:drawable="@drawable/icon_1" />
</selector>
Noe try this on your button.
Noe在你的按钮上尝试这个。
int imgID = getResources().getIdentifier("button_pressed", "drawable", getApplication().getPackageName());
button.setImageResource(imgID);
button_pressed.xml should be in the drawable folder. icon_1_press and icon_1 are two images for button press and normal focus.
button_pressed.xml应该在drawable文件夹中。 icon_1_press和icon_1是按下按钮和正常对焦的两个图像。
#8
1
I am use this code (with ripple effect):
我使用此代码(具有涟漪效应):
<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="@color/color_gray">
<item android:id="@android:id/mask">
<color android:color="@color/color_gray" />
</item></ripple>
#9
1
you can try this code to solve your problem
您可以尝试使用此代码来解决您的问题
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/login_selected" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="@drawable/login_mouse_over" /> <!-- focused -->
<item android:drawable="@drawable/login" /> <!-- default -->
</selector>
write this code in your drawable make a new resource and name it what you want and then write the name of this drwable in the button same as we refer to image src in android
在你的drawable中编写这段代码创建一个新资源,并将其命名为你想要的,然后在按钮中写下这个drwable的名称,就像我们在android中引用图像src一样
#10
0
To deal properly for how long you want to have your button stay in your other color I would advise this version:
为了正确处理您希望按钮保持其他颜色的时间,我建议使用此版本:
button.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
button.setBackground(getResources().getDrawable(R.drawable.on_click_drawable));
break;
case MotionEvent.ACTION_UP:
new java.util.Timer().schedule(
new java.util.TimerTask() {
@Override
public void run() {
((Activity) (getContext())).runOnUiThread(new Runnable() {
@Override
public void run() {
button.setBackground(getResources().getDrawable(R.drawable.not_clicked_drawable));
}
});
}
}, BUTTON_CLICK_TIME_AFTER_RELEASE_ANIMATION);
break;
default:
}
return false;
}
});
BUTTON_CLICK_TIME_AFTER_RELEASE_ANIMATION indicates after how much time [ms] the button will reset to the previous state (however you might want to use some boolean to check that the button hadn't been used in between, depending on what you want to achieve...).
BUTTON_CLICK_TIME_AFTER_RELEASE_ANIMATION指示在按钮将重置为先前状态的时间[ms]之后(但是您可能需要使用一些布尔值来检查按钮之间是否未使用过,具体取决于您要实现的目标...) 。
#11
-1
hai the most easiest way is this:
最简单的方法是:
add this code to mainactivity.java
将此代码添加到mainactivity.java
public void start(View view) {
stop.setBackgroundResource(R.color.red);
start.setBackgroundResource(R.color.yellow);
}
public void stop(View view) {
stop.setBackgroundResource(R.color.yellow);
start.setBackgroundResource(R.color.red);
}
and then in your activity main
然后在你的活动主要
<button android:id="@+id/start" android:layout_height="wrap_content" android:layout_width="wrap_content" android:onclick="start" android:text="Click">
</button><button android:id="@+id/stop" android:layout_height="wrap_content" android:layout_width="wrap_content" android:onclick="stop" android:text="Click">
or follow along this tutorial
或者按照本教程进行操作
#1
114
One approach is to create an XML file like this in drawable
, called whatever.xml:
一种方法是在drawable中创建这样的XML文件,称为whatever.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="@drawable/bgalt" />
<item
android:state_focused="false"
android:state_pressed="true"
android:drawable="@drawable/bgalt" />
<item android:drawable="@drawable/bgnorm" />
</selector>
bgalt
and bgnorm
are PNG images in drawable.
bgalt和bgnormare PNG图像在drawable中。
If you create the buttons programatically in your activity, you can set the background with:
如果在活动中以编程方式创建按钮,则可以使用以下方法设置背景:
final Button b = new Button (MyClass.this);
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.whatever));
If you set your buttons' style with an XML, you would do something like:
如果您使用XML设置按钮的样式,您可以执行以下操作:
<Button
android:id="@+id/mybutton"
android:background="@drawable/watever" />
And finally a link to a tutorial. Hope this helps.
最后是教程的链接。希望这可以帮助。
#2
69
Save this code in drawable folder with "bg_button.xml" and call "@drawable/bg_button" as background of button in your xml:
使用“bg_button.xml”将此代码保存在drawable文件夹中,并将“@ drawable / bg_button”作为xml中按钮的背景调用:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<solid
android:color="#004F81" />
<stroke
android:width="1dp"
android:color="#222222" />
<corners
android:radius="7dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#89cbee"
android:endColor="#004F81"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#4aa5d4" />
<corners
android:radius="7dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
#3
8
Try This
尝试这个
final Button button = (Button) findViewById(R.id.button_id);
button.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP) {
button.setBackgroundColor(Color.RED);
} else if(event.getAction() == MotionEvent.ACTION_DOWN) {
button.setBackgroundColor(Color.BLUE);
}
return false;
}
});
#4
7
Refer this,
参考这个,
boolean check = false;
Button backward_img;
Button backward_img1;
backward_img = (Button) findViewById(R.id.bars_footer_backward);
backward_img1 = (Button) findViewById(R.id.bars_footer_backward1);
backward_img.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
check = true;
backward_img.setBackgroundColor(Color.BLUE);
}
});
if (check == true) {
backward_img1.setBackgroundColor(Color.RED);
backward_img.setBackgroundColor(Color.BLUE);
}
#5
5
If you want to change the backgorund image or color of the button when it is pressed, then just copy this code and paste in your project at exact location described below.
如果要在按下按钮时更改按钮的背景图像或颜色,则只需复制此代码并粘贴到项目中,如下所示。
<!-- Create new xml file like mybtn_layout.xml file in drawable -->
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/pressed" /> <!--pressed -->
<item android:drawable="@drawable/normal" /> <!-- Normal -->
</selector>
<!-- Now this file should be in a drawable folder and use this
single line code in button code to get all the properties of this xml file -->
<Button
android:id="@+id/street_btn"
android:layout_width="wrap_content"
android:background="@drawable/layout_a" > <!-- your required code -->
</Button>
#6
5
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- default -->
<item
android:state_pressed="false"
android:state_focused="false">
<shape
android:innerRadiusRatio="1"
android:shape="rectangle" >
<solid android:color="#00a3e2" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape>
</item>
<!-- button focused -->
<item
android:state_pressed="false"
android:state_focused="true">
<shape
android:innerRadiusRatio="1"
android:shape="rectangle" >
<solid android:color="#5a97f5" />
<padding
android:bottom="5dp"
android:left="10dp"
android:right="10dp"
android:top="5dp" />
</shape></item>
<!-- button pressed -->
<item
android:state_pressed="true"
android:state_focused="false">
<shape
android:innerRadiusRatio="1"
android:shape="rectangle" >
<solid android:color="#478df9"/>
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape></item>
</selector>
#7
4
Try this......
尝试这个......
First create an xml file named button_pressed.xml These are it's contents.
首先创建一个名为button_pressed.xml的xml文件。这些是它的内容。
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"
android:state_pressed="false"
android:drawable="@drawable/icon_1" />
<item android:state_focused="true"
android:state_pressed="true"
android:drawable="@drawable/icon_1_press" />
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="@drawable/icon_1_press" />
<item android:drawable="@drawable/icon_1" />
</selector>
Noe try this on your button.
Noe在你的按钮上尝试这个。
int imgID = getResources().getIdentifier("button_pressed", "drawable", getApplication().getPackageName());
button.setImageResource(imgID);
button_pressed.xml should be in the drawable folder. icon_1_press and icon_1 are two images for button press and normal focus.
button_pressed.xml应该在drawable文件夹中。 icon_1_press和icon_1是按下按钮和正常对焦的两个图像。
#8
1
I am use this code (with ripple effect):
我使用此代码(具有涟漪效应):
<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="@color/color_gray">
<item android:id="@android:id/mask">
<color android:color="@color/color_gray" />
</item></ripple>
#9
1
you can try this code to solve your problem
您可以尝试使用此代码来解决您的问题
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/login_selected" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="@drawable/login_mouse_over" /> <!-- focused -->
<item android:drawable="@drawable/login" /> <!-- default -->
</selector>
write this code in your drawable make a new resource and name it what you want and then write the name of this drwable in the button same as we refer to image src in android
在你的drawable中编写这段代码创建一个新资源,并将其命名为你想要的,然后在按钮中写下这个drwable的名称,就像我们在android中引用图像src一样
#10
0
To deal properly for how long you want to have your button stay in your other color I would advise this version:
为了正确处理您希望按钮保持其他颜色的时间,我建议使用此版本:
button.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
button.setBackground(getResources().getDrawable(R.drawable.on_click_drawable));
break;
case MotionEvent.ACTION_UP:
new java.util.Timer().schedule(
new java.util.TimerTask() {
@Override
public void run() {
((Activity) (getContext())).runOnUiThread(new Runnable() {
@Override
public void run() {
button.setBackground(getResources().getDrawable(R.drawable.not_clicked_drawable));
}
});
}
}, BUTTON_CLICK_TIME_AFTER_RELEASE_ANIMATION);
break;
default:
}
return false;
}
});
BUTTON_CLICK_TIME_AFTER_RELEASE_ANIMATION indicates after how much time [ms] the button will reset to the previous state (however you might want to use some boolean to check that the button hadn't been used in between, depending on what you want to achieve...).
BUTTON_CLICK_TIME_AFTER_RELEASE_ANIMATION指示在按钮将重置为先前状态的时间[ms]之后(但是您可能需要使用一些布尔值来检查按钮之间是否未使用过,具体取决于您要实现的目标...) 。
#11
-1
hai the most easiest way is this:
最简单的方法是:
add this code to mainactivity.java
将此代码添加到mainactivity.java
public void start(View view) {
stop.setBackgroundResource(R.color.red);
start.setBackgroundResource(R.color.yellow);
}
public void stop(View view) {
stop.setBackgroundResource(R.color.yellow);
start.setBackgroundResource(R.color.red);
}
and then in your activity main
然后在你的活动主要
<button android:id="@+id/start" android:layout_height="wrap_content" android:layout_width="wrap_content" android:onclick="start" android:text="Click">
</button><button android:id="@+id/stop" android:layout_height="wrap_content" android:layout_width="wrap_content" android:onclick="stop" android:text="Click">
or follow along this tutorial
或者按照本教程进行操作