按下android按钮状态

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10019367/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:12:49  来源:igfitidea点击:

Pressed android button state

androidbuttonbackgroundstate

提问by Karly

I've been following a tutorial that explains how to use background for a button with different states but it doesn't seem to work :S

我一直在关注一个教程,该教程解释了如何为具有不同状态的按钮使用背景,但它似乎不起作用:S

Here is my code :

这是我的代码:

    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/boutonn" android:state_window_focused="false"/>
    <item android:drawable="@drawable/boutonnpousse" android:state_pressed="true"/>
    <item android:drawable="@drawable/boutonnpousse" android:state_focused="true"/>
    <item android:drawable="@drawable/boutonn" android:state_focused="false" 
    android:state_pressed="false" />

</selector>

This is an xml code that I've placed in my drawable folder, here is a part of the xml of the activity that uses these buttons :

这是我放在 drawable 文件夹中的 xml 代码,这是使用这些按钮的活动的 xml 的一部分:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/backgrounddd"
    android:orientation="vertical" >

            <Button
                android:id="@+id/bNoteRemind"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_gravity="center"
                android:background="@drawable/imagebutton1" /> 
    ...

And here is the java class :

这是java类:

public class MenuPrincipal extends Activity {

    Button NoteRemind;          

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        //on lui associe le layout menuprincipal.xml
        setContentView(R.layout.menuprincipal);

        NoteRemind = (Button) findViewById(R.id.bNoteRemind);     

        // Si on choisit de rédiger une nouvelle task on va être rediriger sur l'activité NoteReminder

        NoteRemind.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

                //On créé l'Intent qui va nous permettre d'afficher l'autre Activity
                //Mettez le nom de l'Activity dans la quelle vous êtes actuellement pour le premier parametre
                v.setPressed(true);

                Intent intent = new Intent(MenuPrincipal.this, NoteReminder.class);
                //Intent intent = new Intent(MenuPrincipal.this, Teste2.class);
                //On démarre l'autre Activity
                startActivity(intent);


            }
        }); ....

The button displays well but when I press it it doesn t show the pressed image :s I don't understand what I am doing wrong !

该按钮显示良好,但当我按下它时,它不显示按下的图像:我不明白我做错了什么!

Does anyone see an error somewhere ???

有没有人在某处看到错误???



Where should I put those lines ? I ve put them in my button xml

我应该把这些线放在哪里?我已经把它们放在我的按钮 xml 中

    <Button
        android:id="@+id/bNoteRemind"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:background="@drawable/imagebutton1"
        android:focusable="true"
        android:focusableInTouchMode="true" />

But now my button background changed to the pressed image without me pressing it :p and it doesn't change

但是现在我的按钮背景变成了按下的图像而我没有按下它:p并且它没有改变

回答by gobernador

Is the Buttonthe only thing you have displayed in your Activity? If so, then it will be focused (triggering the third item in your selector) when the window loads, and you won't be able to navigate away from it. If you want to change only when pressed, delete that third line. While you're at it, delete the first line, as the button will never be pressed when the window isn't focused.

Button您在Activity? 中显示的唯一内容吗?如果是这样,那么selector当窗口加载时它将被聚焦(触发您的 中的第三项),并且您将无法离开它。如果您只想在按下时更改,请删除第三行。当你在它的时候,删除第一行,因为当窗口没有聚焦时按钮永远不会被按下。

In fact, I suggest this code:

实际上,我建议使用以下代码:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/boutonnpousse" android:state_pressed="true"/>
    <item android:drawable="@drawable/boutonn"/>
</selector>