关于Android调用点击事件的三种方式案例。
第一步:在String.xml中配置属性
<resources> <string name="app_name">02Onclick</string> <string name="action_settings">Settings</string> <string name="app_phone">按钮三种点击方式</string> <string name="app_one">第一种</string> <string name="app_two">第二种</string> <string name="app_there">第三种</string> </resources>
第二步:在content_main.xml中设置图形界面
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" 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" android:orientation="vertical" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.example.jikexueyuan.a02onclick.MainActivity" tools:showIn="@layout/activity_main"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_phone" /> <Button android:id="@+id/bt_one" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_one"/> <Button android:id="@+id/b_two" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_two"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_there" android:onClick="getSoce"/> </LinearLayout>
第三步:在MainActivity中添加点击按钮方法 implements View.OnClickListener
package com.example.jikexueyuan.a02onclick; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.View; import android.view.Menu; import android.view.MenuItem; import android.widget.Button; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); //设置侦听按钮One Button bt = (Button) findViewById(R.id.bt_one); bt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { System.out.println("---------第一种方式被调用----------"); } }); //设置侦听按钮Two Button bt2 = (Button) findViewById(R.id.b_two); bt2.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.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(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @Override public void onClick(View view) { System.out.println("---------第二种方式被调用----------"); } public void getSoce(View v){ System.out.println("---------第三种方式被调用----------"); } }