Android的开始之Button按钮控件

Event Listeners:

onClick():通常没有返回值;或返回一个布尔类型的true:当前控件拦截该事件自己处理;false:交给父控件处理

onLongClick()

 

package com.example.android_button;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;
import android.os.Build;

public class MainActivity extends ActionBarActivity implements OnClickListener {
private Button button;
private Button button2;
private Button button3;
private ImageButton imagebutton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
button=(Button)findViewById(R.id.button1);
// button.setOnClickListener(new OnClickListener() {
//
// @Override
// public void onClick(View v) {
// // TODO Auto-generated method stub
// Toast.makeText(MainActivity.this, "点击按钮", 1).show();
// }
// });
button2=(Button)findViewById(R.id.button2);
button3=(Button)findViewById(R.id.button3);
imagebutton=(ImageButton)findViewById(R.id.imageButton1);


button.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
imagebutton.setOnClickListener(this);
// button2.setOnClickListener(new OnClickListener() {
//
// @Override
// public void onClick(View v) {
// // TODO Auto-generated method stub
//
// }
// });
}

@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);
}

/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {

public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
//相应按钮的点击事件
public void sendMessage(View view){

Toast.makeText(MainActivity.this, "相应xml中定义的事件", 1).show();


}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
Toast.makeText(MainActivity.this, "点击按钮1", 1).show();

break;
case R.id.button2:
Toast.makeText(MainActivity.this, "点击按钮2", 1).show();

break;
case R.id.button3:
Toast.makeText(MainActivity.this, "点击按钮3", 1).show();

break;
case R.id.imageButton1:
Toast.makeText(MainActivity.this, "点击imageButton", 1).show();

break;
default:
break;
}
}

}

 

转载于:https://www.cnblogs.com/my334420/p/6440017.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值