第一种点击事件
在xml中设置onclick属性
android:onClick="myOnclick"
第二种;获取Button然后一个一个单独绑定点击事件
http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
< android:id="@+id/btn_imgBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="myOnclick"
android:text="imageButton"
/>
<android:id="@+id/btn_imgView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="myOnclick"
android:text="imageView"
/>
public class MainActivity extends ActionBarActivity {
private Button btnImageBtn;
private Button btnImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnImageBtn = (Button) findViewById(R.id.btn_imgBtn);
btnImageView = (Button) findViewById(R.id.btn_imgView);
btnImageBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "点击ImageButton", Toast.LENGTH_SHORT).show();
}
});
btnImageView.setOnClickListener(new MyListener());
}
第三种:写一个类(MyListener)实现OnClickListener接口,然后Button在设置onclickListener的时候new一个MyListener
btnImageView.setOnClickListener(new MyListener());
class MyListener implements OnClickListener{
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_imgBtn:
Toast.makeText(MainActivity.this, "点击ImageButton", Toast.LENGTH_SHORT).show();
break;
case R.id.btn_imgView:
Toast.makeText(MainActivity.this, "点击imageView", Toast.LENGTH_SHORT).show();
break;
}
第四种:整个类(MianActivity)实现onclickListener的接口
跳转界面
Intent:意图,用于访问android中的组件
用Intent跳转界面(activity)
第一步:new一个Intent()
Intent intent1 = new Intent(MainActivity.this,ImageButtonActivity.class);
startActivity(intent1);
public void myOnclick(View view){
switch (view.getId()) {
case R.id.btn_imgBtn:
Intent intent1 = new Intent(MainActivity.this,ImageButtonActivity.class);
startActivity(intent1);
break;
case R.id.btn_imgView:
Intent intent2 = new Intent(MainActivity.this,ImageViewActivity.class);
startActivity(intent2);
break;
Intent intent = new Intent(当前的activity,跳转到的acticvity.class);
startActivity(intent);
3.ImageView
展示方式:scaleType:
4.ImageButton:
触摸事件:当控件或者屏幕呗触摸的时候,产生的反应
public boolean onTouchEvent(MotionEvent event) {
}
imageButton:现在已经呗button代替,用于展示图片的按钮。不能显示文字。
imageView
scaleType:图片展示的方式
fitStart:展示在控件的上方
fitCenter:展示在控件的中间
fitEnd;展示在控件的下方
fitXY:不按照比例拉伸
matrix:矩阵模式
matrix可以设置图片旋转,缩放。移动
获取图片的高度和宽度
int h = imgView.getDrawable().getIntrinsicHeight();
int w = imgView.getDrawable().getIntrinsicWidth();
Matrix m = new Matrix();
m.postRotate(45);
m.postRotate(45, w/2, h/2);
imgView.setImageMatrix(m);
移动事件:
按下:MotionEvent.ACTION_DOWN
抬起:MotionEvent.ACTION_UP
移动:MotionEvent.ACTION_MOVE
获取当前的移动事件,
event.getAction()
http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
android:id="@+id/img_01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/ss"
android:scaleType="fitXY"/>
android:id="@+id/img_02"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/gl"
android:visibility="gone"
android:scaleType="fitXY"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/bird"
android:layout_gravity="center"
/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#ff0000"
android:gravity="center"
android:text="小鸟飞"/>
public class MainActivity extends Activity {
private ImageView img01;
private ImageView img02;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img01 = (ImageView) findViewById(R.id.img_01);
img02 = (ImageView) findViewById(R.id.img_02);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//System.out.println("被摸了");
if(event.getAction()==MotionEvent.ACTION_UP){
Log.v("TAG", "被抬起来");
if(img01.getVisibility()==View.VISIBLE){
img01.setVisibility(View.GONE);
img02.setVisibility(View.VISIBLE);
}else{
img01.setVisibility(View.VISIBLE);
img02.setVisibility(View.GONE);
}
}else if(event.getAction()==MotionEvent.ACTION_DOWN){
Log.v("TAG", "被按下了");
}else if(event.getAction()==MotionEvent.ACTION_MOVE){
Log.v("TAG", "移动了");
}
return super.onTouchEvent(event);
}
网友评论