Android 如何在单击时对 textview 产生闪烁效果?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23241202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make blink effect on textview on click?
提问by Maxwell
Hi I am new to android development. I want to create onclick effects to textview. When I click on the textview it will blink or something effects make. I tried it with change color, but it's not working. How can I make blink effect on textview onclick ?? please help me with example code. thanks in advance :)
嗨,我是 android 开发的新手。我想为 textview 创建 onclick 效果。当我点击 textview 时,它会闪烁或产生一些效果。我试过改变颜色,但它不起作用。如何在 textview onclick 上制作闪烁效果?请帮助我提供示例代码。提前致谢 :)
回答by kikettas
The easiest way is to set this background in the TextView:
最简单的方法是在TextView 中设置此背景:
android:background="?attr/selectableItemBackground"
And if you want to set a different color for the background, set that attras foreground
instead of background
.
如果您想为背景设置不同的颜色,请将该attr设置为foreground
而不是background
.
回答by Shivansh
create a xml with name something like txt_bg.xml
创建一个名称类似于 txt_bg.xml 的 xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/numpad_button_bg_selected" android:state_selected="true"></item>
<item android:drawable="@drawable/numpad_button_bg_pressed" android:state_pressed="true"></item>
<item android:drawable="@drawable/numpad_button_bg_normal"></item>
</selector>
then add in texview xml
然后添加texview xml
android:background="@drawable/txt_bg"
android:clickable="true"
hope it will help.
希望它会有所帮助。
回答by Juboraj Sarker
try this. it worked for me.
尝试这个。它对我有用。
android:clickable="true"
android:focusable="true"
android:background="?android:attr/selectableItemBackground"
回答by duggu
try below code:-
试试下面的代码:-
<Button
android:id="@+id/action"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:layout_margin="5dp"
android:background="@drawable/btn_click"
android:gravity="center"
android:textColor="@color/white"
android:textSize="12sp" />
btn_click.xml
btn_click.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_hover" android:state_pressed="true"/>
<item android:drawable="@drawable/button"/>
</selector>
or below also
或以下
btn_hover.xml
btn_hover.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke
android:width="1dp"
android:color="#000000" />
<gradient
android:angle="270"
android:centerColor="#1a000000"
android:endColor="#33000000"
android:startColor="@android:color/transparent" >
</gradient>
<corners android:radius="5dp" />
</shape>
btn.xml
文件
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke
android:color="#000000"
android:width="1dp"
/>
<gradient
android:angle="270"
android:centerColor="@android:color/transparent"
android:endColor="@android:color/transparent"
android:startColor="@android:color/transparent" >
</gradient>
<corners android:radius="5dp" />
</shape>
btn_click.xml
btn_click.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/btn_hover" android:state_pressed="true"/>
<item android:drawable="@drawable/btn"/>
</selector>
回答by Digvesh Patel
public class TesteBlinkActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
blink();
}
private void blink(){
final Handler handler = new Handler();
new Thread(new Runnable() {
@Override
public void run() {
int timeToBlink = 1000; //in milissegunds
try{Thread.sleep(timeToBlink);}catch (Exception e) {}
handler.post(new Runnable() {
@Override
public void run() {
TextView txt = (TextView) findViewById(R.id.usage);
if(txt.getVisibility() == View.VISIBLE){
txt.setVisibility(View.INVISIBLE);
}else{
txt.setVisibility(View.VISIBLE);
}
blink();
}
});
}
}).start();
}