Android開(kāi)發(fā)之創(chuàng)建可點(diǎn)擊的Button實(shí)現(xiàn)方法
本文實(shí)例講述了Android創(chuàng)建可點(diǎn)擊的Button實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
感覺(jué)到自己有必要學(xué)習(xí)下手機(jī)開(kāi)發(fā)方面的知識(shí),不論是為了以后的工作需求還是目前的公司項(xiàng)目。
當(dāng)然,任何新東西的開(kāi)始,必然伴隨著第一個(gè)HelloWorld,Android學(xué)習(xí)也不例外。既然才開(kāi)始,我就不做過(guò)多的描述了。
對(duì)于Android開(kāi)發(fā)的IDE:ADT來(lái)說(shuō),打開(kāi)的第一眼有點(diǎn)迷糊,不過(guò)看了網(wǎng)上各種目錄結(jié)構(gòu)的介紹,慢慢的就明白了,做這個(gè)實(shí)例,我們尤其需要關(guān)注兩個(gè)地方,一個(gè)是src目錄,一個(gè)就是res目錄下的layout目錄。src目錄放置的是code-behind源碼,而layout目錄放置的則是xml前臺(tái)配置文件。
既然我們要實(shí)現(xiàn)的功能是點(diǎn)擊按鈕,然后EditText中顯示“Hello World!”。讓我們先打開(kāi)layout文件,拖放一個(gè)Button上去,然后拖放一個(gè)EditText上去,最后的xml文件結(jié)構(gòu)如下:
<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" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:text="Button" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_toRightOf="@+id/button1"
android:text="EditText" />
</RelativeLayout>
然后在后臺(tái)代碼文件中,我們需要引入兩個(gè)命名空間:
import android.widget.Button; import android.widget.EditText;
全部代碼如下:
package com.example.helloworld;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
private Button myButton;
private EditText myText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myButton = (Button)findViewById(R.id.button1);
myText = (EditText)findViewById(R.id.editText1);
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
myText.setText("Hello World!");
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
然后點(diǎn)擊運(yùn)行按鈕,在虛擬機(jī)界面中點(diǎn)擊按鈕,得到的結(jié)果如下圖:

這節(jié)就到這里了,后面我們會(huì)繼續(xù)探秘。
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android調(diào)試技巧與常見(jiàn)問(wèn)題解決方法匯總》、《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android開(kāi)發(fā)設(shè)置RadioButton點(diǎn)擊效果的方法
- Android 點(diǎn)擊ImageButton時(shí)有“按下”的效果的實(shí)現(xiàn)
- Android懸浮按鈕點(diǎn)擊返回頂部FloatingActionButton
- Android Button按鈕的四種點(diǎn)擊事件
- Android開(kāi)發(fā)-之監(jiān)聽(tīng)button點(diǎn)擊事件的多種方法
- Android 自定義Button控件實(shí)現(xiàn)按鈕點(diǎn)擊變色
- Android中button點(diǎn)擊后字體的變色效果
- Android自定義button點(diǎn)擊效果的兩種方式
- Android實(shí)現(xiàn)點(diǎn)擊Button產(chǎn)生水波紋效果
- Android Button點(diǎn)擊事件的四種實(shí)現(xiàn)方法
相關(guān)文章
Android自定義view仿微信刷新旋轉(zhuǎn)小風(fēng)車(chē)
這篇文章主要介紹了Android自定義view仿微信刷新旋轉(zhuǎn)小風(fēng)車(chē),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
Android判斷NavigationBar是否顯示的方法(獲取屏幕真實(shí)的高度)
有些時(shí)候,我們需要知道當(dāng)前手機(jī)上是否顯示了NavigationBar,也就是屏幕底部的虛擬按鍵。這篇文章主要介紹了Android判斷NavigationBar是否顯示的方法(獲取屏幕真實(shí)的高度),需要的朋友可以參考下本文2017-01-01
Android自定義view之仿支付寶芝麻信用儀表盤(pán)示例
本篇文章主要介紹了Android自定義view之仿支付寶芝麻信用儀表盤(pán),具有一定的參考價(jià)值,有興趣的可以了解一下。2017-01-01
Android圖片無(wú)限輪播的實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android圖片無(wú)限輪播的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
Android之RecyclerView輕松實(shí)現(xiàn)下拉刷新和加載更多示例
本篇文章主要介紹了Android之RecyclerView輕松實(shí)現(xiàn)下拉刷新和加載更多示例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2017-02-02
Android編程實(shí)現(xiàn)從字符串中查找電話號(hào)碼的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)從字符串中查找電話號(hào)碼的方法,涉及Android針對(duì)字符串的匹配與查找相關(guān)技巧,需要的朋友可以參考下2016-03-03
Android用tabhost實(shí)現(xiàn) 界面切換,每個(gè)界面為一個(gè)獨(dú)立的activity操作
這篇文章主要介紹了Android用tabhost實(shí)現(xiàn) 界面切換,每個(gè)界面為一個(gè)獨(dú)立的activity操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09
Android中EditText 設(shè)置 imeOptions 無(wú)效問(wèn)題的解決方法
有時(shí)候我們需要在EditText 輸出完之后 需要在鍵盤(pán)出現(xiàn) 右下角變成“Go”或“前往 搜索時(shí);通常我們需要設(shè)置Android:imeOptions屬性,但是今天我發(fā)現(xiàn)設(shè)置了無(wú)效,下面給大家分享下解決方案2016-12-12
Android編程實(shí)現(xiàn)攔截短信并屏蔽系統(tǒng)Notification的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)攔截短信并屏蔽系統(tǒng)Notification的方法,較為詳細(xì)的分析了Android短信與Notification的原理及對(duì)應(yīng)的設(shè)置取消技巧,需要的朋友可以參考下2015-12-12

