AndroidAndroid程序提示和消息button响应事件

it2025-08-23  14

首先,接口XML加入button响应函数 android:onClick="OnMyClick" 

<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_below="@+id/textView1" android:layout_marginTop="30dp" android:onClick="OnMyClick" android:text="Button" /> 然后再在java代码文件中加入响应函数就能够了。

public void Msg1( ) { Toast toast = new Toast(this); Toast toast1 = toast.makeText(this, "Hello World", Toast.LENGTH_LONG); toast1.show(); } public void OnMyClick(View v) { Msg1(); //消息提示显示 }

这样的加入响应函数的方法简单吧,感觉代码可读性比加入setOnClickListener监听用户单击操作要好多了。

setOnClickListener该方法允许代码比较多。太罗嗦。

版权声明:本文博客原创文章。博客,未经同意,不得转载。

转载于:https://www.cnblogs.com/bhlsheji/p/4635533.html

相关资源:数据结构—成绩单生成器
最新回复(0)