android 之 常见事件响应的实现方式对比

来源:互联网 发布:teradata数据库语法 编辑:程序博客网 时间:2024/05/29 21:28

1.动态设置(最常用方式)

package com.example.test_listener;

 

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.view.View;

import android.widget.Button;

import android.widget.Toast;

 

public class MainActivity extends Activity {

 

//回调函数

    @Override

    //给按钮绑定一个listener

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        

        Button button = (Button) findViewById(R.id.button1);

        

        //设置button的单击响应事件

        button.setOnClickListener(new onClickListener(){

 

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

//将响应事件绑定到主视图上,,响应内容为"button被点击了",响应时间为短时间

Toast.makeText(MainActivity.this, "button被点击了", Toast.LENGTH_SHORT).show();

}

 

        });

        

        

    }

 

 

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

    }

    

}

 

 

2.配置方式(仅限于onclick)

 

<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"

    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=".MainActivity" >

 

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/hello_world" />

 

    <Button

        android:id="@+id/button1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentBottom="true"

        android:layout_centerHorizontal="true"

        android:layout_marginBottom="154dp"

        android:text="Button" 

        android:onClick="test" 

        />

 

</RelativeLayout>

 

 

 

============================================================

package com.example.test_listener;

 

import android.os.Bundle;

import android.annotation.SuppressLint;

import android.app.Activity;

import android.view.Menu;

import android.view.View;

 

@SuppressLint("NewApi") public class MainActivity extends Activity {

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

    }

 

 

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

    }

    

    //用于将button每次点击向右移动10个单位

    public void test(View view) {

     view.setX(view.getX() + 10);

    }

}

 

3.可复用方式  {适用于有多个按钮时}

 

package com.example.test_listener;

 

import android.os.Bundle;

import android.annotation.SuppressLint;

import android.app.Activity;

import android.view.*;

import android.widget.*;

 

  public class MainActivity extends Activity {

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        

       Button button =(Button) findViewById(R.id.button1);

       

       button.setOnClickListener(myListener);

    }

 

    private View.OnClickListener myListener = new OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

Toast.makeText(MainActivity.this"hehehe", Toast.LENGTH_SHORT).show();

}

};

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

    }

 

}

 

2.长按事件

        //找到Button

        Button button = (Button)findViewById(R.id.button1);

        

        //设置长按方法

        button.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

System.out.println("Button 长按事件。。。。。");

}

});

3.触发事件

 

0 0
原创粉丝点击