• Android开发入门——Button绑定监听事件三种方式


    import android.app.Activity;

    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;

    public class AActivity extends Activity {
    /** Called when the activity is first created. */

    EditText Ev1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Ev1 = (EditText)findViewById(R.id.editText1);

    //第一种方式
    Button Btn1 = (Button)findViewById(R.id.button1);//获取按钮资源
    Btn1.setOnClickListener(new Button.OnClickListener(){//创建监听
    public void onClick(View v) {
    String strTmp = "点击Button01";
    Ev1.setText(strTmp);
    }

    });

    //第二种方式
    Button Btn2 = (Button) findViewById(R.id.button2);//获取按钮资源
    Btn2.setOnClickListener(listener);//设置监听

    }

    Button.OnClickListener listener = new Button.OnClickListener(){//创建监听对象
    public void onClick(View v){
    String strTmp="点击Button02";
    Ev1.setText(strTmp);
    }

    };


    //第三种方式
    public void Btn3OnClick(View view){
    String strTmp="点击Button03";
    Ev1.setText(strTmp);

    }
    }

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />

    <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button1" />

    <Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button2" />

    <Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button3"
    android:onClick="Btn3OnClick"/>

    <EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <requestFocus />
    </EditText>

    </LinearLayout>

  • 相关阅读:
    03_ if 练习 _ little2big
    uva 11275 3D Triangles
    uva 12296 Pieces and Discs
    uvalive 3218 Find the Border
    uvalive 2797 Monster Trap
    uvalive 4992 Jungle Outpost
    uva 2218 Triathlon
    uvalive 3890 Most Distant Point from the Sea
    uvalive 4728 Squares
    uva 10256 The Great Divide
  • 原文地址:https://www.cnblogs.com/wuditju/p/4337662.html
Copyright © 2020-2023  润新知