Android編程基礎(chǔ)之簡單Button事件響應(yīng)綜合提示控件Toast應(yīng)用示例
本文實(shí)例講述了Android簡單Button事件響應(yīng)綜合提示控件Toast應(yīng)用。分享給大家供大家參考,具體如下:
前面講述了在main.xml里定義了Button對象,這里我們來學(xué)習(xí)Button如何實(shí)現(xiàn)事件響應(yīng)。
Button按鈕所觸發(fā)的事件處理,我們稱之為Event Handle,只不過在Android當(dāng)中,按鈕事件是由系統(tǒng)的Button.OnClickListener所控制,熟悉Java程序設(shè)計(jì)的讀者對OnXxxListener應(yīng)該不陌生.以下的Demo,我們將實(shí)現(xiàn)當(dāng)點(diǎn)擊Button時,TextView文字將發(fā)生改變,并在屏幕上出現(xiàn)一段時間的Toast提醒.
讓我們看一下效果圖:
點(diǎn)擊按鈕前:
點(diǎn)擊按鈕后:
我們主要在程序里改了兩處地方一處是main.xml 另一處是ButtonDemo.java
Main.xml 代碼如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" //1.5以后默認(rèn)的是LinearLayout布局 android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/textview1" //定義Id方便Java類找到它,并且控制它 android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/button1" android:layout_width="60px" android:layout_height="wrap_content" android:layout_gravity="right" //讓Button放在右面 android:text="確定" /> </LinearLayout>
Button.java 代碼如下:
package com.android.test; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class ButtonDemo extends Activity { private TextView textview1; private Button button1; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //通過ID在找到定義在main.xml里的TextView和Button控件 textview1 = (TextView)findViewById(R.id.textview1); button1 = (Button)findViewById(R.id.button1); //增加事件響應(yīng) button1.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) { //Toast提示控件 Toast.makeText(ButtonDemo.this, "TextView里的文字發(fā)生了改變,你注意到了嗎?", Toast.LENGTH_LONG).show(); //將TextView的文字發(fā)生改變 textview1.setText("歡迎來到魏祝林的博客!"); } }); } }
今天就到此為止。
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進(jìn)階教程》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android操作SQLite數(shù)據(jù)庫技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android數(shù)據(jù)庫操作技巧總結(jié)》、《Android文件操作技巧匯總》、《Android編程開發(fā)之SD卡操作方法匯總》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計(jì)有所幫助。
- Android編程之Button控件配合Toast控件用法分析
- Android定制RadioButton樣式三種實(shí)現(xiàn)方法
- Android點(diǎn)擊Button實(shí)現(xiàn)功能的幾種方法總結(jié)
- Android 控件(button)對齊方法實(shí)現(xiàn)詳解
- Android自定義格式顯示Button的布局思路
- Android實(shí)現(xiàn)點(diǎn)擊Button產(chǎn)生水波紋效果
- Android中的Button自定義點(diǎn)擊效果實(shí)例代碼
- android之自定義Toast使用方法
- android自定義toast(widget開發(fā))示例
- android開發(fā)教程之實(shí)現(xiàn)toast工具類
相關(guān)文章
Android自定義listview布局實(shí)現(xiàn)上拉加載下拉刷新功能
這篇文章主要介紹了Android自定義listview布局實(shí)現(xiàn)上拉加載下拉刷新功能,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-12-12Android實(shí)現(xiàn)頁面跳轉(zhuǎn)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)頁面跳轉(zhuǎn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-06-06Android Lottie實(shí)現(xiàn)中秋月餅變明月動畫特效實(shí)例
Lottie是Airbnb開源的一個支持 Android、iOS 以及 ReactNative,利用json文件的方式快速實(shí)現(xiàn)動畫效果的庫,下面這篇文章主要給大家介紹了關(guān)于Android Lottie實(shí)現(xiàn)中秋月餅變明月動畫特效的相關(guān)資料,需要的朋友可以參考下2021-09-09Android 加載大圖及多圖避免程序出現(xiàn)OOM(OutOfMemory)異常
這篇文章主要介紹了Android 加載大圖及多圖避免程序出現(xiàn)OOM(OutOfMemory)異常的相關(guān)資料,需要的朋友可以參考下2017-03-03Android 錄制手機(jī)屏幕視頻生成GIF圖片實(shí)例詳解
這篇文章主要介紹了Android 錄制手機(jī)屏幕視頻生成GIF圖片實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-03-03android自定義波浪加載動畫的實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了android自定義波浪加載動畫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-11-11