Android开发案例Onclick点击事件switch调用分类04

本文详细介绍了在Android应用开发中,如何在MainActivity.java中处理Button的Onclick点击事件,并结合switch语句进行不同功能的调用。首先在strings.xml文件中配置Button的文字,然后在MainActivity.java中编写相应的事件监听代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一步:设置sting.xml中配置Button。

<resources>
    <string name="app_name">04Onclicks</string>
    <string name="action_settings">Settings</string>
    <string name="app_phone">点击按钮事件展示</string>
    <string name="app_one">第一种</string>
    <string name="app_two">第二种</string>
    <string name="app_there">第三种</string>
    <string name="app_android">第三种</string>
    <string name="app_iphone">第四种</string>
</resources>


第二步:设置content_mian.xml

<?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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.jikexueyuan.a04onclicks.MainActivity"
    tools:showIn="@layout/activity_main">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_phone" />
    <Button
        android:id="@+id/bt_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_one"/>
    <Button
        android:id="@+id/bt_two"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_two"/>
    <Button
        android:id="@+id/bt_there"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_there"
        android:onClick="getSoce"/>
    <Button
        android:id="@+id/bt_android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_android"
        android:onClick="getSoce"/>
    <Button
        android:id="@+id/bt_iphone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_iphone"
        android:onClick="getSoce"/>
</LinearLayout>

第三步:设置MainActivity.java

public void getSoce(View v){
    int id = v.getId();
    switch (id){
        case R.id.bt_there:
            System.out.println("第三种事件被点击");
            break;
        case R.id.bt_android:
            System.out.println("安卓");
            break;
        case R.id.bt_iphone:
            System.out.println("苹果");
            break;

    }

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值