package com.example.wang.testapp2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Main2Activity
extends AppCompatActivity {
TextView tv_1;
Button bt_2; //方法二的内容
Button bt_31;
//方法三的内容
Button bt_32;
//方法三的内容
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
tv_1=
(TextView)findViewById(R.id.tv_1);
//方法二 给bt_2设置监听事件
bt_2=(Button)findViewById(R.id.bt_2);
//方法二的内容
bt_2.setOnClickListener(new View.OnClickListener() {
//方法二的内容
@Override
public void onClick(View v) {
//方法二的内容
tv_1.setText("方法二");
//方法二的内容
}
//方法二的内容
});
//方法二的内容
bt_31=(Button)findViewById(R.id.bt_31);
//方法三的内容
bt_32=(Button)findViewById(R.id.bt_32);
//方法三的内容
bt_OnClickListener bt1=
new bt_OnClickListener();
//方法三的内容
bt_31.setOnClickListener(bt1);
//方法三的内容
bt_32.setOnClickListener(bt1);
//方法三的内容
}
//方法一:直接用onclick方法
public void bt_1(View v)
{
tv_1.setText("方法一"
);
}
//方法三
//内部类实现OnClicklistenner接口
class bt_OnClickListener
implements View.OnClickListener
//方法三的内容
{
@Override
//v 事件源
public void onClick(View v) {
//方法三的内容
//转成按钮
Button bt=(Button)v;
//方法三的内容
//获得按钮上的文字
String str=bt.getText().toString();
//方法三的内容
//处理事件的业务逻辑 设置显示文字
tv_1.setText(str);
//方法三的内容
}
}
}
Main2Activity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="com.example.wang.testapp2.Main2Activity"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#ccc"
android:gravity="center_vertical"
android:textSize="25sp"
android:id="@+id/tv_1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="方法一"
android:id="@+id/bt_1"
android:onClick="bt_1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="方法二"
android:id="@+id/bt_2"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="方法三-1"
android:id="@+id/bt_31"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="方法三-2"
android:id="@+id/bt_32"/>
</LinearLayout>
activity_main2
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.wang.testapp2">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Main2Activity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ZuoyeActivity" />
<activity android:name=".MainActivity"></activity>
</application>
A
</manifest>
AndroidManifest
转载于:https://www.cnblogs.com/wangchuanqi/p/5457436.html
相关资源:Android OnClickListener监听接口用法范例.rar