加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xml的onclick

发布时间:2020-12-16 05:26:03 所属栏目:百科 来源:网络整理
导读:布局: ?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:layo

布局:

<?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/btn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="按钮1"
android:onClick="mOnButton"/>

<Button
android:id="@+id/btn2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="按钮2"
android:onClick="mOnButton"/>

</LinearLayout>


主界面:

package com.issay;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


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




private Button btn1,btn2;

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




btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
}


public void mOnButton(View v) {


switch (v.getId()) {
case R.id.btn1:
Toast.makeText(this,"按钮1",Toast.LENGTH_LONG).show();
break;
case R.id.btn2:
Toast.makeText(this,"按钮2",Toast.LENGTH_LONG).show();
break;
}
}
}

效果图如下:点击按钮1时弹出Toast显示按钮1.点击按钮2相同

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读