【问题标题】:How to ad an effect to an ImageButton on an android app?如何在 Android 应用程序上向 ImageButton 广告效果?
【发布时间】:2015-01-07 03:47:12
【问题描述】:

我目前在我的 android 应用程序项目中添加了两个 ImageButton,但是当我在我的 android 设备上按 then 时,按钮不会缩进。我怎样才能给它添加一个效果,使它在按下时看起来更像一个按钮?

我的完整源代码如下所示:

    final TextView text2 = (TextView) findViewById(R.id.textView2);
    text2.setText("");
    final TextView text = (TextView) findViewById(R.id.textView1);
    text.setText("");
    final Button button3 = (Button) findViewById(R.id.button1);
    button3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            clicked1 = 0;
            clicked2 = 0;
            text2.setText("  " + clicked1 + " SHOTS ");
            text.setText("  " + clicked2 + " CUPS ");
        }
    });

    final TextView text1 = (TextView) findViewById(R.id.textView2);
    text1.setText("  0 SHOTS");
    final ImageButton button2 = (ImageButton) findViewById(R.id.imageButton2);
    button2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            clicked2++;
            text1.setText("  " + clicked2 + " SHOTS ");
        }
    });
    final TextView text3 = (TextView) findViewById(R.id.textView1);
    text3.setText("  0 CUPS");
    final ImageButton button = (ImageButton)findViewById(R.id.imageButton1);
    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            clicked1++;
            text3.setText("  " + clicked1 + " CUPS ");

        }


    });

}

}

【问题讨论】:

    标签: android eclipse button indentation effect


    【解决方案1】:

    您可以非常轻松地做到这一点,让我们尝试在您的 ImageButtons 上使用简单的按钮并执行以下操作


    创建一个xml文件并将其保存在项目中的drawable文件夹之一中,让bg.xml,将其设置为按钮的背景

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

    其中 button_pressedbutton_notpressed 是您希望在点击转换和按钮正常视图时显示的图像

    在你的activity的xml文件中,设置按钮的背景为bg.xml

    <Button
            android:id="@+id/btn"
            android:layout_width="150dp"
            android:layout_height="60dp"
            android:textSize="20dp"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="450dp"
            android:background="@drawable/bg"  <--! set it like this-->
            android:text="Submit"
            android:textColor="#FFFFFF" /> 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-08
      • 2014-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-01
      • 2017-05-01
      • 1970-01-01
      相关资源
      最近更新 更多