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

歡迎訪問 生活随笔!

生活随笔

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

Android

Android应用开发基础篇(1)-----Button

發布時間:2025/3/15 Android 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android应用开发基础篇(1)-----Button 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

鏈接地址:http://www.cnblogs.com/lknlfy/archive/2012/02/14/2351740.html

一、概述

? ? ? ?Button,顧名思義就是按鈕的意思,它主要的功能是響應用戶按下按鈕時的動作。

二、應用

? ? ?新建一個工程,名字為MyButton,在/res/layout/main.xml文件中添加以下內容:

1 <Button
2 android:id="@+id/button"
3 android:layout_width="fill_parent"
4 android:layout_height="wrap_content"
5 android:text="Click"
6 />

添加后main.xml文件的內容為:

1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:orientation="vertical" >
6
7 <TextView
8 android:layout_width="fill_parent"
9 android:layout_height="wrap_content"
10 android:text="@string/hello" />
11
12 <Button
13 android:id="@+id/button"
14 android:layout_width="fill_parent"
15 android:layout_height="wrap_content"
16 android:text="Click"
17 />
18
19 </LinearLayout>

接著修改MyButtonActivity.java文件,在MyButtonActivity類里聲明一個Button對象mButton

?

private Button mButton = null;

在onCreate()函數里通過findViewById()函數實例化mButton

?

mButton = (Button)findViewById(R.id.button);

緊接著編寫mButton的監聽函數

?

mButton.setOnClickListener(new View.OnClickListener()
{

@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
PlayToast("You Clicked Button");
}
});

其中PlayToast()函數是通過Toast類用來顯示"You Clicked Button"這串字符串的,比較簡單,如下所示:

private void PlayToast(String s)
{
Toast toast = Toast.makeText(this, s , Toast.LENGTH_LONG);
toast.show();
}

好了。下面是MyButtonActivity.java文件的完整內容:

1 package com.nan.button;
2
3 import android.app.Activity;
4 import android.os.Bundle;
5 import android.view.View;
6 import android.widget.Button;
7 import android.widget.Toast;
8
9
10
11 public class MyButtonActivity extends Activity
12 {
13 private Button mButton = null;
14
15
16 /** Called when the activity is first created. */
17 @Override
18 public void onCreate(Bundle savedInstanceState)
19 {
20 super.onCreate(savedInstanceState);
21 setContentView(R.layout.main);
22
23 mButton = (Button)findViewById(R.id.button);
24 mButton.setOnClickListener(new View.OnClickListener()
25 {
26
27 @Override
28 public void onClick(View v)
29 {
30 // TODO Auto-generated method stub
31 PlayToast("You Clicked Button");
32 }
33 });
34
35 }
36
37
38 private void PlayToast(String s)
39 {
40 Toast toast = Toast.makeText(this, s , Toast.LENGTH_LONG);
41 toast.show();
42 }
43
44 }

運行程序,并點擊按鈕,效果如下:

?

?

?

?

?


?

標簽:?Android開發

轉載于:https://www.cnblogs.com/wvqusrtg/p/5139317.html

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的Android应用开发基础篇(1)-----Button的全部內容,希望文章能夠幫你解決所遇到的問題。

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