zoukankan      html  css  js  c++  java
  • OnClickListener接口

    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

  • 相关阅读:
    [转]CROSS APPLY 和outer apply 的区别
    第一次WCF部署成功心得(UP+证书)
    .NET 简单实现广播
    【转】 c#注册全局快捷键
    软件人才成长链
    [转]关于VS2005智能设备中无法找到PInvoke DLL问题
    [转]我倡导无政府主义编程—Fred George访谈录
    运行Windows Mobile程序时报错:无法找到 PInvoke DLL SQLite.Interop.065.DLL
    系统分析师考试说明
    [转]Oracle 字符集的查看和修改
  • 原文地址:https://www.cnblogs.com/wangchuanqi/p/5457436.html
Copyright © 2011-2022 走看看