Android開發(fā)之創(chuàng)建可點擊的Button實現(xiàn)方法
本文實例講述了Android創(chuàng)建可點擊的Button實現(xiàn)方法。分享給大家供大家參考,具體如下:
感覺到自己有必要學習下手機開發(fā)方面的知識,不論是為了以后的工作需求還是目前的公司項目。
當然,任何新東西的開始,必然伴隨著第一個HelloWorld,Android學習也不例外。既然才開始,我就不做過多的描述了。
對于Android開發(fā)的IDE:ADT來說,打開的第一眼有點迷糊,不過看了網(wǎng)上各種目錄結(jié)構(gòu)的介紹,慢慢的就明白了,做這個實例,我們尤其需要關注兩個地方,一個是src目錄,一個就是res目錄下的layout目錄。src目錄放置的是code-behind源碼,而layout目錄放置的則是xml前臺配置文件。
既然我們要實現(xiàn)的功能是點擊按鈕,然后EditText中顯示“Hello World!”。讓我們先打開layout文件,拖放一個Button上去,然后拖放一個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>
然后在后臺代碼文件中,我們需要引入兩個命名空間:
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; } }
然后點擊運行按鈕,在虛擬機界面中點擊按鈕,得到的結(jié)果如下圖:
這節(jié)就到這里了,后面我們會繼續(xù)探秘。
更多關于Android相關內(nèi)容感興趣的讀者可查看本站專題:《Android調(diào)試技巧與常見問題解決方法匯總》、《Android開發(fā)入門與進階教程》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設計有所幫助。
- Android開發(fā)設置RadioButton點擊效果的方法
- Android 點擊ImageButton時有“按下”的效果的實現(xiàn)
- Android懸浮按鈕點擊返回頂部FloatingActionButton
- Android Button按鈕的四種點擊事件
- Android開發(fā)-之監(jiān)聽button點擊事件的多種方法
- Android 自定義Button控件實現(xiàn)按鈕點擊變色
- Android中button點擊后字體的變色效果
- Android自定義button點擊效果的兩種方式
- Android實現(xiàn)點擊Button產(chǎn)生水波紋效果
- Android Button點擊事件的四種實現(xiàn)方法
相關文章
Android UI控件RatingBar實現(xiàn)自定義星星評分效果
這篇文章主要為大家詳細介紹了Android UI控件RatingBar實現(xiàn)自定義星星評分效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02Android內(nèi)嵌Unity并實現(xiàn)互相跳轉(zhuǎn)的實例代碼
這篇文章主要介紹了Android內(nèi)嵌Unity并實現(xiàn)互相跳轉(zhuǎn)的實例代碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11Android簡單實現(xiàn) 緩存數(shù)據(jù)
這篇文章主要介紹了Android簡單實現(xiàn) 緩存數(shù)據(jù),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11Android Studio 全屏沉浸式透明狀態(tài)欄效果的實現(xiàn)
這篇文章主要介紹了Android Studio 全屏沉浸式透明狀態(tài)欄效果,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11SwipeRefreshLayout+RecyclerView實現(xiàn)上拉刷新和下拉刷新功能
這篇文章主要介紹了SwipeRefreshLayout+RecyclerView實現(xiàn)上拉刷新和下拉刷新功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-01-01Android應用開發(fā)中實現(xiàn)apk皮膚文件換膚的思路分析
這篇文章主要介紹了Android應用開發(fā)中實現(xiàn)apk皮膚文件換膚的思路分析,包括布局和主要的皮膚更換邏輯實現(xiàn),需要的朋友可以參考下2016-02-02Android應用圖標在狀態(tài)欄上顯示實現(xiàn)原理
Android應用圖標在狀態(tài)欄上顯示,以及顯示不同的圖標,其實很研究完后,才發(fā)現(xiàn),很簡單,具體實現(xiàn)如下,感興趣的朋友可以參考下哈2013-06-06