findVIEwByID(R.ID.home_button).setonClickListener( new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { Toast.makeText(MainActivity.this, "点击按钮", Toast.LENGTH_LONG).show(); } });
源码app/src/main/java/com/yyshu/demo1/MainActivity.javapackage com.yyshu.demo1;import androIDx.appcompat.app.AppCompatActivity;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.Widget.Toast;public class MainActivity extends AppCompatActivity { @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); findVIEwByID(R.ID.home_button).setonClickListener( new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { Toast.makeText(MainActivity.this, "点击按钮", Toast.LENGTH_LONG).show(); } } ); }}
方法2 通过androID:onClick
绑定注:需要xml视图层增加
androID:onClick="button_test1"
public voID button_test1(VIEw vIEw) { Toast.makeText(MainActivity.this, "点击按钮2", Toast.LENGTH_LONG).show();}
方法3 继承VIEw.OnClickListener
,所有按钮通过onClick触发注:需要
implements VIEw.OnClickListener
继承
package com.yyshu.demo1;import androIDx.appcompat.app.AppCompatActivity;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.Toast;public class MainActivity extends AppCompatActivity implements VIEw.OnClickListener { private button mbutton; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); mbutton = (button) findVIEwByID(R.ID.home_button); mbutton.setonClickListener(this); } public voID onClick(VIEw v) { switch (v.getID()) { case R.ID.home_button: Toast.makeText(MainActivity.this, "点击按钮3", Toast.LENGTH_LONG).show(); break; } }}
视图UI层app/src/main/res/layout/activity_main.xml<?xml version="1.0" enCoding="utf-8"?><androIDx.constraintlayout.Widget.ConstraintLayout 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" tools:context=".MainActivity"> <TextVIEw androID:ID="@+ID/home_Title" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_margintop="16dp" androID:text="@string/home_Title" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constrainttop_totopOf="parent" app:layout_constraintVertical_bias="0.0" /> <button androID:ID="@+ID/home_button" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_margintop="16dp" androID:text="@string/home_bitton" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constrainttop_toBottomOf="@+ID/home_Title" app:layout_constraintVertical_bias="0.0" /></androIDx.constraintlayout.Widget.ConstraintLayout>
app/src/main/res/values/strings.xml<resources> <string name="app_name">DEMO1</string> <string name="home_Title">你好世界!</string> <string name="home_bitton">按钮</string></resources>
总结 以上是内存溢出为你收集整理的【Android】触发按钮的三种方式全部内容,希望文章能够帮你解决【Android】触发按钮的三种方式所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)