效果图如下:
button单击处理事件的三种方法:
1.匿名类:
leftTopBut=(Button)findViewById(R.id.left_top_but);
leftTopBut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "left_top_but clicked", Toast.LENGTH_SHORT).show();
}
});
2.内部实现接口OnClickListener:
public class MainActivity extends Activity implements OnClickListener
{
private Button leftBottomBut;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
leftBottomBut=(Button)findViewById(R.id.left_bottom_but);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.left_bottom_but:
Toast.makeText(this, "left_bottom_but clicked", Toast.LENGTH_SHORT).show();
break;
case R.id.right_bottom_but:
Toast.makeText(this, "right_bottom_but clicked", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}
3.直接在layout.xml中调用Activity提供的public方法:
activity中提供如下函数
public void butclicked(View v)
{
switch(v.getId())
{
case R.id.right_top_but:
Toast.makeText(this, "right_top_but clicked", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
layout.xml中调用方法:
<Button
android:id="@+id/right_bottom_but"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_below="@id/centerBut"
android:layout_toRightOf="@id/centerBut"
android:background="#ff00ff00"
android:rotationY="-45"
android:text="rightBottom" //这里
android:textColor="#ffff0000" /
源码如下:
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.liang.test1.MainActivity" >
<Button
android:id="@+id/centerBut"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerInParent="true"
android:visibility="invisible"
android:text="hehe" />
<Button
android:id="@+id/left_top_but"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_above="@id/centerBut"
android:layout_toLeftOf="@id/centerBut"
android:background="#ff00ff00"
android:rotationX="-35"
android:rotationY="35"
android:text="leftTop" />
<Button
android:id="@+id/right_top_but"
android:layout_toRightOf="@id/centerBut"
android:layout_above="@id/centerBut"
android:layout_width="100dp"
android:layout_height="50dp"
android:rotation="75"
android:background="@drawable/ic_launcher"
android:textColor="#75ff0000"
android:text="rightTop"
android:onClick="butclicked"/>
<Button
android:id="@+id/left_bottom_but"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_below="@id/centerBut"
android:layout_toLeftOf="@id/centerBut"
android:background="#ff00ff00"
android:text="leftBottom"
android:rotationY="45"
android:textColor="#75ff0000" />
<Button
android:id="@+id/right_bottom_but"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_below="@id/centerBut"
android:layout_toRightOf="@id/centerBut"
android:background="#ff00ff00"
android:rotationY="-45"
android:text="rightBottom"
android:textColor="#ffff0000" />
</RelativeLayout>
MainActivity.java
package com.liang.test1;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
private Button leftBottomBut;
private Button rightBottomBut;
private Button leftTopBut;
// private Button rightTopBut;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
leftBottomBut=(Button)findViewById(R.id.left_bottom_but);
leftBottomBut.setOnClickListener(this);
rightBottomBut=(Button)findViewById(R.id.right_bottom_but);
rightBottomBut.setOnClickListener(this);
leftTopBut=(Button)findViewById(R.id.left_top_but);
leftTopBut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "left_top_but clicked", Toast.LENGTH_SHORT).show();
}
});
// rightTopBut=(Button)findViewById(R.id.right_top_but);
// rightTopBut.setOnClickListener(this);
}
@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) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.left_bottom_but:
Toast.makeText(this, "left_bottom_but clicked", Toast.LENGTH_SHORT).show();
break;
case R.id.right_bottom_but:
Toast.makeText(this, "right_bottom_but clicked", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
public void butclicked(View v)
{
switch(v.getId())
{
case R.id.right_top_but:
Toast.makeText(this, "right_top_but clicked", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}