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

}

 

posted @ 2017-02-24 19:42  张兮兮  阅读(4395)  评论(0编辑  收藏  举报