Android編程之Button控件配合Toast控件用法分析
本文實(shí)例講述了Android編程之Button控件配合Toast控件用法。分享給大家供大家參考,具體如下:
在本章教程中,我們將會(huì)學(xué)習(xí)Button控件的使用,同時(shí)順便說一下Toast提示控件。
在Android程序開發(fā)中,我們使用最多的用戶交互控件可能就是Button的了,而我們使用最多的事件估計(jì)也就是onclick事件了。
這些事件也是最簡單的事件,我們一般通過google自帶的API接口就可以調(diào)用了,我們具體看看怎么做吧。
第一步。新建一個(gè)工程Ep.Toast,活動(dòng)和主視圖名稱我都使用默認(rèn)的了,設(shè)置一下視圖activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 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" tools:context=".MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignTop="@+id/textView1" android:layout_marginRight="61dp" android:text="確定" /> </RelativeLayout>
里面就是一個(gè)文字標(biāo)簽和一個(gè)按鈕控件。
第二步。我們寫核心文件——MainActivity.java,在這里我會(huì)盡量多給你們注釋:
package com.example.ep.toast; import android.os.Bundle; import android.app.Activity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private TextView txtview1; private Button btn1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //綁定文字標(biāo)簽控件 txtview1=(TextView)findViewById(R.id.textView1); //綁定按鈕控件 btn1=(Button)findViewById(R.id.button1); //添加一個(gè)點(diǎn)擊事件監(jiān)聽器并實(shí)例化一個(gè)點(diǎn)擊事件的監(jiān)聽 btn1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //點(diǎn)擊按鈕后設(shè)置文字標(biāo)簽 txtview1.setText("Joven"); //點(diǎn)擊按鈕后彈出提示框,里面的參數(shù)是(綁定活動(dòng),提示內(nèi)容,顯示時(shí)間) Toast.makeText(MainActivity.this, "Joven", Toast.LENGTH_LONG).show(); } }); } }
最后我們就可以看看效果了。
好了本章就講到這里了,這些雖然都是些基礎(chǔ)得不能再基礎(chǔ)得東西。但是不管多么龐大而復(fù)雜的項(xiàng)目,其實(shí)都是在這些東西上面建立起來的。知識(shí)都是積累才會(huì)變精華。
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- android之自定義Toast使用方法
- Android控件系列之Toast使用介紹
- Android 5.0以上Toast不顯示的解決方法
- android自定義toast(widget開發(fā))示例
- android開發(fā)教程之實(shí)現(xiàn)toast工具類
- Android Service中使用Toast無法正常顯示問題的解決方法
- Android Toast的用法總結(jié)(五種用法)
- 超簡單實(shí)現(xiàn)Android自定義Toast示例(附源碼)
- Android超實(shí)用的Toast提示框優(yōu)化分享
- Android實(shí)現(xiàn)Toast提示框圖文并存的方法
- Android編程實(shí)現(xiàn)Toast只顯示最后一條的方法
相關(guān)文章
Android 再按一次退出程序的實(shí)現(xiàn)方法
抽空研究了一下"再按一次退出程序"的實(shí)現(xiàn)方式,直接上代碼(請(qǐng)自動(dòng)忽略無關(guān)代碼)2014-01-01Android drawable微技巧,你不知道的drawable細(xì)節(jié)
今天小編就為大家分享一篇關(guān)于Android drawable微技巧,你不知道的drawable細(xì)節(jié),小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-10-10Android實(shí)現(xiàn)銀行卡號(hào)掃描識(shí)別功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)銀行卡號(hào)掃描識(shí)別功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09Android自定義HorizontalScrollView打造超強(qiáng)Gallery效果
這篇文章主要介紹了Android自定義HorizontalScrollView打造圖片橫向滑動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05Android實(shí)現(xiàn)iPhone晃動(dòng)撤銷輸入功能 Android仿微信搖一搖功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)iPhone晃動(dòng)撤銷輸入功能,Android仿微信搖一搖功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07使用Android WebSocket實(shí)現(xiàn)即時(shí)通訊功能
即時(shí)通訊(Instant Messaging)最重要的毫無疑問就是即時(shí),不能有明顯的延遲,要實(shí)現(xiàn)IM的功能其實(shí)并不難,目前有很多第三方,比如極光的JMessage,都比較容易實(shí)現(xiàn)。本文通過實(shí)例代碼給大家分享Android WebSocket實(shí)現(xiàn)即時(shí)通訊功能,一起看看吧2019-10-10Android?studio實(shí)現(xiàn)簡單計(jì)算器的編寫
這篇文章主要為大家詳細(xì)介紹了Android?studio實(shí)現(xiàn)簡單計(jì)算器的編寫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05