ImageButton与Button
16lz
2021-01-25
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的属性来实现。
更多相关文章
- Android动画TweenAnimation的使用方法
- Android(安卓)ueventd解析
- Android(安卓)百度地图 计算两点之间的距离
- Android(安卓)输入系统(二)EventHub
- Android(安卓)9.0 System.getProperty("java.library.path") 源
- Android事件分发,拦截,处理机制
- Android(安卓)EditText文本输入框禁止输入空格
- Android:UI控件风格与主题、selector、Theme
- android 实现ImageView按压效果和解决背景图片拉申问题