zoukankan      html  css  js  c++  java
  • 4.2设计具有背景图的按钮—ImageButton的焦点及事件处理

    目录

    4.2设计具有背景图的按钮—ImageButton的焦点及事件处理... 1

    目标:1

    方法:1

    代码:1

    效果:3

    问题:... 3

     

     

    目标:设计具有背景图的按钮

    方法:ImageButton onFocusChange方法,onClick方法

    代码:

    package edu.cquptzx.ImageButton;

    import android.app.Activity;

    import android.os.Bundle;

    import android.view.View;

    import android.view.View.OnClickListener;

    import android.view.View.OnFocusChangeListener;

    import android.widget.Button;

    import android.widget.ImageButton;

    import android.widget.TextView;

     

    publicclass ImageButtonActivity extends Activity {

        private ImageButton ib;

        private Button btn;

        private TextView tv;

        /** Called when the activity is first created. */

        publicvoid onCreate(Bundle savedInstanceState)

        {

            super.onCreate(savedInstanceState);

            setContentView(R.layout.main);

           

            /*Find the objects by IDs.*/

            ib = (ImageButton) findViewById(R.id.imageButton);

            btn= (Button) findViewById(R.id.button);

            tv = (TextView) findViewById(R.id.statusText);

           

            final  String str_textView = getString(R.string.textView);

            final  String str_status_FOCUSED = getString(R.string.status_FOCUSED);

            final  String str_status_LOSTFOCUSED = getString(R.string.status_LOSTFOCUSED);

            final  String str_GOTCLICK = getString(R.string.status_GOTCLICK);

     

           

            /*Add an listener to the ImageButton to response the OnFocus Event. */

            ib.setOnFocusChangeListener(new OnFocusChangeListener()

          

                 {

                      publicvoid onFocusChange(View v, boolean hasFocus)

                      {

                         /*IF ImageButton's status change , replace the pic and text. */

                         System.out.println("A");

                         if( v.isFocused() == true)

                         {

                             System.out.println("B");

                             tv.setText(str_textView + str_status_FOCUSED);

                             ib.setImageResource(R.drawable.iconfull);

                         }

                         else

                         {

                             System.out.println("C");

                             tv.setText(str_textView + str_status_LOSTFOCUSED);

                             ib.setImageResource(R.drawable.iconempty);

                         }                   

                      }                  

                 });

            ib.setOnClickListener(new OnClickListener()

                   {

                      publicvoid onClick(View v)

                      {

                         System.out.println("D");

                         tv.setText(str_textView + str_GOTCLICK);

                         ib.setImageResource(R.drawable.iconfull);

                      }             

                   });

            btn.setOnClickListener(new OnClickListener()

                   {

                      publicvoid onClick(View v)

                      {  

                         System.out.println("E");

                         tv.setText(str_textView + str_status_LOSTFOCUSED);

                         ib.setImageResource(R.drawable.iconempty);

                      }             

                   });

           

        }

    }

    效果:

    image

    image

    问题:

    onFocusChange方法并没有起到作用,从LogCat里面可以看出来:

    SystemOut

  • 相关阅读:
    MySql 用户 及权限操作
    MAC 重置MySQL root 密码
    在mac系统安装Apache Tomcat的详细步骤[转]
    Maven:mirror和repository 区别
    ES6 入门系列
    转场动画CALayer (Transition)
    OC 异常处理
    Foundation 框架
    Enum枚举
    Invalid App Store Icon. The App Store Icon in the asset catalog in 'xxx.app' can’t be transparent nor contain an alpha channel.
  • 原文地址:https://www.cnblogs.com/xilifeng/p/2655699.html
Copyright © 2011-2022 走看看