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

    }

  • 相关阅读:
    快速瘦身大米瘦身法 健康程序员,至尚生活!
    20个生活小习惯"狠"减肥 健康程序员,至尚生活!
    橙子帮助减肥,谁都可以轻松做到 健康程序员,至尚生活!
    10个减肥小贴士,日常必备哦! 健康程序员,至尚生活!
    有源淹没分析arcgis_ArcGIS水文分析实战教程(15)库容和淹没区计算
    maven:apache.poi下载不了
    springboot:@RequestBody 注解只能处理json格式的请求字符串吗?
    arcpy计算统计值
    arcgis栅格计算器
    GeoTools:polygon 转 multipolygon&遍历multipolygon中的polygon
  • 原文地址:https://www.cnblogs.com/my334420/p/6440017.html
Copyright © 2020-2023  润新知