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

  • 相关阅读:
    e621. Activating a Keystroke When Any Child Component Has Focus
    e587. Filling Basic Shapes
    e591. Drawing Simple Text
    e595. Drawing an Image
    e586. Drawing Simple Shapes
    e636. Listening to All Key Events Before Delivery to Focused Component
    在 PL/SQL 块的哪部分可以对初始变量赋予新值? (选择1项)
    Oracle数据库中,在SQL语句中连接字符串的方法是哪个?(选择1项)
    你判断下面语句,有什么作用?(单选)
    Oracle数据库表空间与数据文件的关系描述正确的是( )
  • 原文地址:https://www.cnblogs.com/xilifeng/p/2655699.html
Copyright © 2011-2022 走看看