日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android学习笔记篇1. 从按钮的点击事件开始

發布時間:2023/12/19 Android 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android学习笔记篇1. 从按钮的点击事件开始 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

布局:

在XML文件中寫三個按鈕,給它們不同的id:
(為按鈕2綁定點擊方法click:android:onClick=“click”)

<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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"android:orientation="vertical"android:padding="8dp"tools:context=".MainActivity"><Buttonandroid:id="@+id/btn_one"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="按鈕1"/><Buttonandroid:id="@+id/btn_two"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="按鈕2"android:onClick="click"/><Buttonandroid:id="@+id/btn_three"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="按鈕3"/></LinearLayout>

在Java中編寫代碼,實現三種類型的按鈕的點擊事件:
(詳細看注釋)

package com.example.no_1;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle; import android.view.View; import android.widget.Button;public class MainActivity extends AppCompatActivity implements View.OnClickListener {private Button btn_one, btn_two, btn_three;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);btn_one = (Button)findViewById(R.id.btn_one);btn_two = (Button)findViewById(R.id.btn_two);btn_three = (Button)findViewById(R.id.btn_three);btn_three.setOnClickListener(this);/*** 使用匿名內部類,實現按鈕1的點擊*/btn_one.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {btn_one.setText("按鈕1被點擊");}});}/*** 以控件按鈕2中的Onclick屬性值click* 為名創建的點擊方法,實現按鈕2的點擊* @param view*/public void click(View view) {btn_two.setText("按鈕2被點擊");}/*** 實現OnClickListener接口中的方法onClick* 實現按鈕3的點擊* @param view*/@Overridepublic void onClick(View view) {switch (view.getId()) {case R.id.btn_three:btn_three.setText("按鈕3被點擊");}} }

運行,點擊三個按鈕,顯示設定文字:

總結

以上是生活随笔為你收集整理的Android学习笔记篇1. 从按钮的点击事件开始的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。