【问题标题】:Pressed android button state按下 android 按钮状态
【发布时间】:2014-11-03 18:23:54
【问题描述】:

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

这是我的代码:

    <?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>

这是我放置在可绘制文件夹中的 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" /> 
    ...

这里是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);


            }
        }); ....

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

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


我应该把这些行放在哪里?我已将它们放在我的按钮 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" />

但现在我的按钮背景更改为按下的图像,而我没有按下它:p 并且它没有改变

【问题讨论】:

标签: android button background state


【解决方案1】:

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

其实我建议这样的代码:

<?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>

【讨论】:

  • 按下时如何改变动画速度?
猜你喜欢
  • 1970-01-01
  • 2015-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多