1.Button控件

Butotn控件,主要用来实现一些命令操作,通过注册监听事件来实现。首先需要在xml文档中放入一个button按钮。

1     <Button2         android:id="@+id/button1"3         android:layout_width="180dp"4         android:layout_height="64dp"5         android:layout_x="45dp"6         android:layout_y="269dp"7         android:background="@drawable/btn01"8         android:text="Button" />
Button

可以在xml 中设置该控件的相关属性,包括layout_width等。后台实现的代码为

 1 public class MainActivity extends Activity { 2  3     Button myButton; 4     ImageButton myImg; 5     TextView textView; 6     @Override 7     protected void onCreate(Bundle savedInstanceState) { 8         super.onCreate(savedInstanceState); 9         setContentView(R.layout.activity_main);10         myButton=(Button)findViewById(R.id.button1);11         textView=(TextView)findViewById(R.id.text1);12         myButton.setOnClickListener(new OnClickListener(){13 14             @Override15             public void onClick(View v) {16                 // TODO 自动生成的方法存根17                 textView.setText("wo lai le");18             Toast.makeText(MainActivity.this, "别惦记我", 5000).show();19             }});20     }21 22 23     @Override24     public boolean onCreateOptionsMenu(Menu menu) {25         // Inflate the menu; this adds items to the action bar if it is present.26         getMenuInflater().inflate(R.menu.main, menu);27         return true;28     }29     30 }
Button事件

这里通过findViewById()来获取该button控件,为该控件实现setOnClickListener()方法,该方法可以响应button的单击事件。Toast.makeText()来实现弹出提示语言。

2、ImageButton控件

ImageButton控件,可以作为button的另外一种扩展,他可以drawable中显示图标。

ImageButton控件的注册。

1     <ImageButton2         android:id="@+id/imageButton1"3         android:layout_width="60dp"4         android:layout_height="wrap_content"5         android:layout_x="86dp"6         android:layout_y="145dp"7         android:background="@drawable/easyicon_net_24"8         android:src="@drawable/imgbutton" />
ImageButton

可以在注册的时候,设置该控件的各种属性。

本文实现的主要是ImageButton控件的状态改变的时候,变换不同的图片。当鼠标单击的时候显示不同的图片。我们需要在drawable文件夹中加入一个xml文档。

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

该文档用来注册imagebutton的不同状态,通过设置android:state_pressed的属性来实现。

更多相关文章

  1. Android动画TweenAnimation的使用方法
  2. Android(安卓)ueventd解析
  3. Android(安卓)百度地图 计算两点之间的距离
  4. Android(安卓)输入系统(二)EventHub
  5. Android(安卓)9.0 System.getProperty("java.library.path") 源
  6. Android事件分发,拦截,处理机制
  7. Android(安卓)EditText文本输入框禁止输入空格
  8. Android:UI控件风格与主题、selector、Theme
  9. android 实现ImageView按压效果和解决背景图片拉申问题

随机推荐

  1. 最近状态不咋好 ...
  2. Java后端开发学习路线「光头强版」发布
  3. 一文学会 Node.js 中的流[每日前端夜话0x
  4. 常用 Linux 发行版操作系统大盘点!
  5. Java中static变量作用和用法详解
  6. 不就看一下Java后端开发书架吗?这有啥不行
  7. 9 个强大的 JavaScript 小技巧[每日前端
  8. 从 JavaScript、ES6、ES7 到 ES10,你学到
  9. 用 globalThis 访问全局对象[每日前端夜
  10. 生产环境下的 Node.js 日志记录方案[每日