Android java ImageButton 方法帮助

发布于 2024-11-18 08:59:45 字数 1323 浏览 7 评论 0原文

所以我正在一步一步地解决这个问题。我的按钮就可以按照我想要的方式切换。但现在我想添加更多按钮。

       `public class Menu extends Activity{
        ImageButton select;    
        int isClicked = 0;

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

                select = (ImageButton)findViewById(R.id.select);
        select.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                     if (isClicked == 0){
                        select.setImageResource(R.drawable.select_pressed);
                        isClicked = 1;
                     }
                     else{
                        select.setImageResource(R.drawable.select);
                        isClicked = 0;
                     }
                }});
             }
          }`

假设我要复制 ImageButton 方法。如果我要使用新按钮的代码,我到底应该在哪里插入它?

    `<ImageButton 
android:src="@drawable/select"
android:id="@+id/select"
android:layout_height="30dp"
android:layout_width="120dp"
android:background="@null"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true">
    </ImageButton>`

So I'm figuring this stuff out in baby steps. And just got my button to toggle the way i want it. But now i want to add more buttons.

       `public class Menu extends Activity{
        ImageButton select;    
        int isClicked = 0;

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

                select = (ImageButton)findViewById(R.id.select);
        select.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                     if (isClicked == 0){
                        select.setImageResource(R.drawable.select_pressed);
                        isClicked = 1;
                     }
                     else{
                        select.setImageResource(R.drawable.select);
                        isClicked = 0;
                     }
                }});
             }
          }`

So say i were to copy that ImageButton method. Where exactly would i insert it, if i were going to use the code for a new button?

    `<ImageButton 
android:src="@drawable/select"
android:id="@+id/select"
android:layout_height="30dp"
android:layout_width="120dp"
android:background="@null"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true">
    </ImageButton>`

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

辞慾 2024-11-25 08:59:45

您可以使用按钮上的标签属性来存储状态。然后,您可以将状态逻辑放在一个单独的方法中,如下所示:

public void changeState(View v) {
                     if (v.getTag() == "false"){
                        ((ImageButton)v).setImageResource(R.drawable.select_pressed);
                        v.setTag("true");
                     }
                     else{
                        ((ImageButton)v).setImageResource(R.drawable.select);
                        v.setTag("false");
                     }
                }});

您可以从每个 ImageButton 的点击侦听器中调用该方法,而无需在应用程序上下文中保存大量 ImageButton 的状态。

You can use the tag attribute on the button to store the state. Then you could put your state logic in a separate method like this:

public void changeState(View v) {
                     if (v.getTag() == "false"){
                        ((ImageButton)v).setImageResource(R.drawable.select_pressed);
                        v.setTag("true");
                     }
                     else{
                        ((ImageButton)v).setImageResource(R.drawable.select);
                        v.setTag("false");
                     }
                }});

This method you could call from each ImageButton's clicklistener, without saving the state of numerous ImageButtons in the application context.

您可以使用 main.xml 文件中的图形布局编辑器轻松创建图像按钮。
为新按钮创建一个新的图像按钮对象,并像您所做的那样在 onCreate() 函数中指定单击侦听器。

You can easily create image buttons using the graphical layout editor in you main.xml file.
Create a new image button object for your new buttons and specify the on click listeners in the onCreate() function as you have done.

夏末染殇 2024-11-25 08:59:45

Android 有一种称为选择器的东西,可以根据视图的状态自动更改背景资源,而不是您的方法。选择器在单独的可绘制 xml 文件中定义,并从使用状态的视图的 xml 声明中引用。例如:
选择器(android_button.xml):

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

按钮:

<Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:background="@drawable/android_button" />

您仍然需要为每个按钮实现一个点击侦听器,但是您将拥有一个可以在您想要的所有按钮中重用的选择器,并且表示逻辑已与业务逻辑分离你的应用程序。

此答案中显示的源代码直接来自 http://developer .android.com/resources/tutorials/views/hello-formstuff.html#CustomButton
您可以在其中阅读更完整的解释。

Instead of your approach, Android has something called selectors that can change the background resource automatically depending on the state of the view. Selectors are defined in a separate drawable xml file and referenced from the xml declaration of the view that utilizes the states. For example:
Selector (android_button.xml):

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

Button:

<Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:background="@drawable/android_button" />

You will still have to implement a clicklistener for each button, but you will have a selector that you can reuse in all the buttons you want, and the presentation logic has been separated from the business logic of your app.

The source code shown in this answer comes straight from http://developer.android.com/resources/tutorials/views/hello-formstuff.html#CustomButton
where you can read a more complete explanation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文