<span style="font-family: Arial, Helvetica, sans-serif;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"</span>
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.buttonproject.MainActivity" >
<TextView
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/btn_ok"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btn_ok" />
<Button
android:id="@+id/btn_cancel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btn_cancel" />
<ImageButton
android:id="@+id/imageBtin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/aaa" />
</LinearLayout>
package com.example.buttonproject;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity implements OnClickListener{
private Button btn_ok,btn_cancel;
private TextView tv;
private ImageButton Ibtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_ok=(Button)findViewById(R.id.btn_ok);
btn_cancel=(Button)findViewById(R.id.btn_cancel);
tv = (TextView)findViewById(R.id.tv);
btn_ok.setOnClickListener(this);
btn_cancel.setOnClickListener(this);
Ibtn=(ImageButton)findViewById(R.id.imageBtin);
//为图片按钮添加触屏监听
Ibtn.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
//当前用户为按下
if(event.getAction()==MotionEvent.ACTION_DOWN){
//设置图片按钮背景图
Ibtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.bbb));
}else if(event.getAction()==MotionEvent.ACTION_UP){//用户当前为抬起
Ibtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.aaa));
}
return false;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
if(v==btn_ok){
tv.setText("确定按钮触发事件!");
}else if(v==btn_cancel){
tv.setText("取消按钮触发事件!");
}
}
}