Android開(kāi)發(fā)基礎(chǔ)與案例實(shí)戰(zhàn) 課件 童長(zhǎng)飛 第2、3章 Android的布局與基本UI、列表與適配器_第1頁(yè)
Android開(kāi)發(fā)基礎(chǔ)與案例實(shí)戰(zhàn) 課件 童長(zhǎng)飛 第2、3章 Android的布局與基本UI、列表與適配器_第2頁(yè)
Android開(kāi)發(fā)基礎(chǔ)與案例實(shí)戰(zhàn) 課件 童長(zhǎng)飛 第2、3章 Android的布局與基本UI、列表與適配器_第3頁(yè)
Android開(kāi)發(fā)基礎(chǔ)與案例實(shí)戰(zhàn) 課件 童長(zhǎng)飛 第2、3章 Android的布局與基本UI、列表與適配器_第4頁(yè)
Android開(kāi)發(fā)基礎(chǔ)與案例實(shí)戰(zhàn) 課件 童長(zhǎng)飛 第2、3章 Android的布局與基本UI、列表與適配器_第5頁(yè)
已閱讀5頁(yè),還剩89頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

Android的布局與基本UI

2.1任務(wù)1-按鈕Button與文本TextView的互動(dòng)21線性布局的初步使用2按鈕點(diǎn)擊事件處理3Toast的使用4隨機(jī)整數(shù)的使用線性布局LinearLayout方向控制android:orientationvertical,垂直線性布局,依次從上往下放horizontal,水平線性布局,依次從左往右方尺寸屬性android:layout_widthandroid:layout_heightmatch_parent與父視圖(父容器)匹配尺寸(占據(jù)父容器的剩余空間)wrap_content包圍內(nèi)容,寬或高由內(nèi)容決定絕對(duì)值,使用相對(duì)像素,例如100dp注意事項(xiàng)當(dāng)尺寸屬性設(shè)置為match_parent,將使得同級(jí)后續(xù)UI被擠出容器不可見(jiàn)3常用快捷鍵1Ctrl+Shift+Space(Ctrl+Alt+Space):代碼智能自動(dòng)填充。Alt+Enter:快速修復(fù)錯(cuò)誤,使用時(shí)鼠標(biāo)須定位到提示錯(cuò)誤的地方。Alt+Insert:代碼生成器,例如構(gòu)造方法、類(lèi)成員變量的getter和setter方法、方法重寫(xiě)、接口實(shí)現(xiàn)、toString()方法等。Ctrl+Alt+T:調(diào)出try-catch、if、for等代碼模板,先選中需要使用模板的代碼,再使用快捷鍵。Ctrl+D:快速?gòu)?fù)制當(dāng)前行代碼。Ctrl+鼠標(biāo)左鍵:跳轉(zhuǎn)到定義處。Ctrl+Alt+左箭頭:跳轉(zhuǎn)到上次編輯處。Ctrl+Alt+V:將當(dāng)前賦值或者方法調(diào)用的返回值賦為局部變量(V:Variable),使用頻率極高,大大提高編程效率。Ctrl+Alt+F:將當(dāng)前賦值或者方法調(diào)用的返回值賦為成員變量(F:Field)。Ctrl+Alt+M:將選中的代碼塊提取成一個(gè)方法(M:Method)。4常用快捷鍵2Ctrl+P:查看方法的參數(shù)信息。Ctrl+Q:調(diào)出幫助信息,例如方法的使用文檔和參數(shù)說(shuō)明,返回值定義等。Shift+F6智能重命名,相關(guān)變量或者類(lèi)會(huì)自動(dòng)改名。Ctrl+/:當(dāng)前行代碼注釋與反注釋。Ctrl+Shift+/:選中代碼塊注釋與反注釋。Ctrl+Alt+L:代碼格式化,將代碼縮進(jìn)等調(diào)整得更加美觀。Ctrl+O:顯示父類(lèi)所有可改寫(xiě)的方法,包括接口方法。Ctrl+Alt+O:優(yōu)化代碼導(dǎo)入包,刪除未被使用的包。F8:?jiǎn)尾秸{(diào)試(StepOver),當(dāng)前代碼單行執(zhí)行,若是方法調(diào)用,則執(zhí)行方法直至方法返回。F7:?jiǎn)尾秸{(diào)試(StepInto),若是單行賦值代碼與F8相同,若是方法調(diào)用,則會(huì)跳入方法,執(zhí)行所調(diào)用方法的首行代碼并暫停。Alt+F9:執(zhí)行到光標(biāo)處。Alt+F8:彈出表達(dá)式窗口,可對(duì)對(duì)象進(jìn)行表達(dá)式運(yùn)算。F9:全速運(yùn)行,若遇到下一個(gè)斷點(diǎn),則暫停到下一個(gè)斷點(diǎn),在調(diào)試中常和F8、斷點(diǎn)等配合使用。5按鈕Button的點(diǎn)擊事件偵聽(tīng)直接對(duì)Button對(duì)象設(shè)置點(diǎn)擊偵聽(tīng),并匿名實(shí)現(xiàn)點(diǎn)擊偵聽(tīng)接口對(duì)MainActivity類(lèi)寫(xiě)B(tài)utton的偵聽(tīng)接口實(shí)現(xiàn),并將偵聽(tīng)接口指向MainActivity實(shí)例在XML中設(shè)置Button的點(diǎn)擊偵聽(tīng)方法6bt.setOnClickListener(newView.OnClickListener(){

@Override

publicvoidonClick(Viewview){

//點(diǎn)擊事件處理

}

});publicclassMainActivityextendsAppCompatActivityimplementsView.OnClickListener{

@Override

protectedvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.my_main);

Buttonbt=findViewById(R.id.button);

bt.setOnClickListener(this);

}

@Override

publicvoidonClick(Viewv){

//Button點(diǎn)擊回調(diào)

}

}publicclassMainActivityextendsAppCompatActivity{

@Override

protectedvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.my_main);

}

publicvoidbuttonOnclick(Viewview){

//XML中定義的Button點(diǎn)擊回調(diào)方法

}

}<Button…android:onClick="buttonOnClick"…/>文本框不可編輯文本框TextViewandroid:textColor設(shè)置字體顏色(引用顏色或者#RRGGBB)android:textSize設(shè)置字體大?。▎挝皇褂胹p)android:autoLink智能文本鏈接phone識(shí)別電話號(hào)碼web識(shí)別網(wǎng)址all識(shí)別所有鏈接可編輯文本框EditTextandroid:ems在寬度為wrap_content時(shí)生效,控制寬度,單位為字符android:inputType設(shè)置輸入類(lèi)型android:hint設(shè)置提示信息,當(dāng)EditText清空時(shí)顯示hint信息Toast彈出提示框Toast.makeText(android.content.Contextcontext,CharSequencetext,intduration).show()context:指MainActivity.thistext:顯示的內(nèi)容duration:Toast.LENGTH_LONG,Toast.LENGTH_SHORT7任務(wù)1實(shí)現(xiàn)-布局my_main.xml8<LinearLayoutxmlns:android="/apk/res/android"

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent">

<!--根節(jié)點(diǎn):垂直的線性布局,占據(jù)整個(gè)屏幕高度和寬度-->

<TextView

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="你的個(gè)人信息"/>

<!--文本框,寬度與線性容器同寬,等同于與屏幕同寬,由內(nèi)容決定高度-->

<!--注釋只能在UI標(biāo)簽之外,可用“ctr+/”快捷鍵-->

<Button

android:id="@+id/button"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="生成隨機(jī)數(shù)"/>

<!--按鈕Button,并使用android:id屬性創(chuàng)建了id,在代碼中可用R.id.button引用該UI-->

<TextView

android:id="@+id/tv_result"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text=""/>

<!--第二個(gè)TextView在代碼中可用R.id.tv_result引用-->

</LinearLayout>任務(wù)1實(shí)現(xiàn)-MainActivity.java9publicclassMainActivityextendsAppCompatActivity{

intcount=0;

//TextViewtv=findViewById(R.id.tv_result);

@Override

protectedvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.my_main);

//設(shè)置布局文件,將res/layout/my_main.xml作為布局文件

//通過(guò)setContentView()方法設(shè)置視圖之后才能通過(guò)findViewById()從Activity視圖中找UI

finalTextViewtv=findViewById(R.id.tv_result);

//R.id.tv_result是my_main.xml布局文件中的一個(gè)TextView

//使用final關(guān)鍵詞是因?yàn)閠v是局部變量,只能在onCreate()中訪問(wèn)

//若要在該函數(shù)的匿名函數(shù)中訪問(wèn),可將tv設(shè)為全局變量或者final局部變量

Buttonbt=findViewById(R.id.button);

bt.setOnClickListener(newView.OnClickListener(){

@Override

publicvoidonClick(Viewview){

inti=newRandom().nextInt(1000);//利用隨機(jī)函數(shù)產(chǎn)生一個(gè)[0,999]的整型隨機(jī)數(shù)

tv.setText(""+i);//字符串“”與i通過(guò)+拼接運(yùn)算,自動(dòng)將i變量轉(zhuǎn)為對(duì)應(yīng)字符串

Strings=String.format("第%d次按鈕",++count);//String.format相當(dāng)于C語(yǔ)言的sprintf

Toast.makeText(MainActivity.this,s,Toast.LENGTH_SHORT).show();//Toast顯示內(nèi)容

}

});

}

}Android項(xiàng)目的存儲(chǔ)瘦身10刪除的目錄.gradle(注意不是gradle).ideaapp/build瘦身效果能大大減小項(xiàng)目大小Android項(xiàng)目的出錯(cuò)排查11publicclassMainActivityextendsAppCompatActivity{

intcount=0;

TextViewtv=findViewById(R.id.tv_result);

@Override

protectedvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.my_main);

Buttonbt=findViewById(R.id.button);

bt.setOnClickListener(newView.OnClickListener(){

@Override

publicvoidonClick(Viewview){

inti=newRandom().nextInt(1000);//利用隨機(jī)函數(shù)產(chǎn)生一個(gè)[0,999]的整型隨機(jī)數(shù)

tv.setText(""+i);//字符串“”與i通過(guò)+拼接運(yùn)算,自動(dòng)將i變量轉(zhuǎn)為對(duì)應(yīng)字符串

Strings=String.format("第%d次按鈕",++count);//String.format相當(dāng)于C語(yǔ)言的sprintf

Toast.makeText(MainActivity.this,s,Toast.LENGTH_SHORT).show();//Toast顯示內(nèi)容

}

});

}

}利用Logcat窗口能看到程序出錯(cuò)的原因和出錯(cuò)處的代碼(藍(lán)色鏈接)本程序出錯(cuò),tv不能在成員變量中初始化,因?yàn)榇藭r(shí)尚未調(diào)用onCreate()方法,沒(méi)有視圖,無(wú)法找到相關(guān)UI,導(dǎo)致tv為null2.2任務(wù)2-布局對(duì)齊gravity與layout_gravity121UI內(nèi)部對(duì)齊gravity2UI與父容器對(duì)齊layout_gravity3layout_gravity的使用條件父容器方向與對(duì)齊方向正交對(duì)齊常用屬性left:左對(duì)齊;right:右對(duì)齊;start:取決于語(yǔ)言習(xí)慣,若是從左到右的文字順序則等效于左對(duì)齊,反之則右對(duì)齊;end:與start相對(duì)應(yīng),為start的相反方向;top:頂部對(duì)齊;bottom:底部對(duì)齊;center:上下左右居中對(duì)齊;center_horizontal:水平居中對(duì)齊;center_vertical:垂直居中對(duì)齊。13任務(wù)2實(shí)現(xiàn)-布局文件my_main.xml14<LinearLayoutxmlns:android="/apk/res/android"

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent">

<TextView

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="YournameandID"/>

<Button

android:id="@+id/button"

android:layout_width="200dp"

android:layout_height="wrap_content"

android:layout_gravity="right"

android:text="Layout_gravity"/>

<Button

android:id="@+id/button2"

android:layout_width="200dp"

android:gravity="right"

android:layout_height="wrap_content"

android:text="Gravity"/>

</LinearLayout>思考將第一個(gè)Button通過(guò)layout_gravity設(shè)置為底部對(duì)齊,能否實(shí)現(xiàn)?為什么?2.3任務(wù)3-UI的權(quán)重控制151layout_weight控制寬度或者高度由什么決定2權(quán)重控制的是剩余空間權(quán)重控制什么時(shí)候失效3如何實(shí)現(xiàn)UI的絕對(duì)等比分配不同寬度下權(quán)重的控制效果16<LinearLayoutxmlns:android="/apk/res/android"

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent">

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

<EditText

android:id="@+id/editTextTextPersonName"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:inputType="textPersonName"

android:text="1234567890"/>

<EditText

android:id="@+id/editTextTextPersonName2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:inputType="textPersonName"

android:text="1"/>

</LinearLayout><LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

<EditText

android:id="@+id/editTextTextPersonName3"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:ems="10"

android:inputType="textPersonName"

android:text="1234567890"/>

<EditText

android:id="@+id/editTextTextPersonName4"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:ems="10"

android:inputType="textPersonName"

android:text="1"/>

</LinearLayout>

</LinearLayout>任務(wù)3的布局文件my_main.xml17<?xmlversion="1.0"encoding="utf-8"?>

<LinearLayoutxmlns:android="/apk/res/android"

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent">

<EditText

android:id="@+id/et1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="To"

android:ems="10"

android:inputType="phone"

android:text=""/>

<!--android:inputType="phone"控制文本框鍵盤(pán)為電話號(hào)碼型鍵盤(pán)-->

<EditText

android:id="@+id/et2"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:ems="10"

android:hint="Subject"

android:inputType="text"

android:text=""/>

<!--android:inputType="text"控制文本框?yàn)槲谋炬I盤(pán)--><EditText

android:id="@+id/et3"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:ems="10"

android:hint="Message"

android:gravity="top"

android:inputType="text"

android:layout_weight="3"

android:text=""/>

<!--et3與TextView通過(guò)權(quán)重控制,按3:1占據(jù)線性布局剩余高度-->

<Button

android:id="@+id/button"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="send"/>

<TextView

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="YournameandID"/>

</LinearLayout>2.4任務(wù)4-單選框RadioButton181RadioGroup容器RadioButton單選框2RadioGroup事件處理3findViewById()靈活使用RadioGroup的使用要點(diǎn)android:orientation屬性horizontal控制組內(nèi)RadioButton按水平方向依次放置RadioButton往往還需要layout_weight屬性控制寬度權(quán)重vertical控制組內(nèi)RadioButton按垂直方向依次放置事件處理setOnCheckedChangeListener()方法,偵聽(tīng)選中狀態(tài)改變偵聽(tīng)回調(diào)方法

publicvoidonCheckedChanged(RadioGroupgroup,intcheckedId)checkedId:被選中的RadioButton的id,可配合findViewById()方法得到選中的對(duì)象19實(shí)現(xiàn)布局my_main.xml20<LinearLayoutxmlns:android="/apk/res/android"

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent">

<TextView

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="YournameandID"/>

<RadioGroup

android:id="@+id/radio_group"

android:layout_width="match_parent"

android:orientation="horizontal"

android:layout_height="wrap_content">

<!--須給RadioGroup創(chuàng)建id屬性,設(shè)置方向?qū)傩詾樗椒较?,注意高度是wrap_content-->

<RadioButton

android:id="@+id/rb1"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="爬山"/>

<RadioButton

android:id="@+id/rb2"

android:layout_width="0dp"

android:layout_weight="1"

android:layout_height="wrap_content"

android:text="跑步"/>

<RadioButton

android:id="@+id/rb3"

android:layout_width="0dp"

android:layout_weight="1"

android:layout_height="wrap_content"

android:text="游泳"/>

<!--利用layout_width="0dp"和android:layout_weight="1"控制UI寬度比例-->

</RadioGroup>

<TextView

android:id="@+id/tv_result"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text=""/>

</LinearLayout>實(shí)現(xiàn)MainActivity21publicclassMainActivityextendsAppCompatActivity{

@Override

protectedvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.my_main);

RadioGrouprg=findViewById(R.id.radio_group);

//利用RadioGroup對(duì)選中事件進(jìn)行偵聽(tīng)

TextViewtv=findViewById(R.id.tv_result);

//AndroidStudio2021版中,變量tv無(wú)需聲明為final

rg.setOnCheckedChangeListener(newRadioGroup.OnCheckedChangeListener(){

@Override

publicvoidonCheckedChanged(RadioGroupgroup,intcheckedId){

RadioButtonrb=findViewById(checkedId);

//checkedId是被選中的RadioButton對(duì)象的id

//利用findViewById()方法和參數(shù)checkedId,關(guān)聯(lián)對(duì)應(yīng)RadioButton

tv.setText(rb.getText());

//通過(guò)RadioButton對(duì)象的getText()方法獲取文本并賦給tv

}

});

}

}2.5任務(wù)5-多選框CheckBox221多選框與單選框的區(qū)別2CheckBox對(duì)象響應(yīng)事件3MainActivity響應(yīng)CheckBox事件實(shí)現(xiàn)布局my_main.xml23<LinearLayoutxmlns:android="/apk/res/android"

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent">

<TextView

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="YournameandID"/>

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

<!--注意水平LinearLayout的高度是wrap_content-->

<CheckBox

android:id="@+id/cb1"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="爬山"/>

<CheckBox

android:id="@+id/cb2"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="跑步"/>

<CheckBox

android:id="@+id/cb3"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="游泳"/>

</LinearLayout><TextView

android:id="@+id/tv_result"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text=""/>

</LinearLayout>思考問(wèn)題如果內(nèi)嵌的LinearLayout高度采用match_parent會(huì)發(fā)生什么現(xiàn)象?方法1-實(shí)現(xiàn)MainActivity24publicclassMainActivityextendsAppCompatActivity{

CheckBoxcb1,cb2,cb3;

//在onCreate()方法之外訪問(wèn),定義成員變量更合適

TextViewtv;

@Override

protectedvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.my_main);

tv=findViewById(R.id.tv_result);

cb1=findViewById(R.id.cb1);

cb2=findViewById(R.id.cb2);

cb3=findViewById(R.id.cb3);

cb1.setOnCheckedChangeListener(

newCompoundButton.OnCheckedChangeListener(){

//CheckBox的選中狀態(tài)偵聽(tīng)和事件回調(diào)

@Override

publicvoidonCheckedChanged(CompoundButtonbuttonView,

booleanisChecked){

updateCheckBox();

//選中待要封裝成方法的代碼塊,使用Ctrl+Alt+M快捷鍵

}

});

cb2.setOnCheckedChangeListener(

newCompoundButton.OnCheckedChangeListener(){

@Override

publicvoidonCheckedChanged(CompoundButtonbuttonView,

booleanisChecked){

updateCheckBox();

}

});

cb3.setOnCheckedChangeListener(

newCompoundButton.OnCheckedChangeListener(){

@Override

publicvoidonCheckedChanged(CompoundButtonbuttonView,

booleanisChecked){

updateCheckBox();

}

});

}

privatevoidupdateCheckBox(){

//將3個(gè)CheckBox選中狀態(tài)更新到TextView的處理程序,封裝成方法,方便調(diào)用

Strings="";

if(cb1.isChecked()){//判斷cb1是否被選中,true為被選中

s+=cb1.getText()+"";

//如果cb1選中,取對(duì)應(yīng)文本,拼接到變量s,字符串間加空格間隔

}

if(cb2.isChecked()){

s+=cb2.getText()+"";

}

if(cb3.isChecked()){

s+=cb3.getText();

}

tv.setText(s);

}

}方法2-實(shí)現(xiàn)MainActivity25publicclassMainActivityextendsAppCompatActivity

implementsCompoundButton.OnCheckedChangeListener{

//可輸入implementsCheckBox.OnCheckedChangeListener,開(kāi)發(fā)環(huán)境會(huì)自動(dòng)幫助修改

CheckBoxcb1,cb2,cb3;

TextViewtv;

@Override

protectedvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.my_main);

tv=findViewById(R.id.tv_result);

cb1=findViewById(R.id.cb1);

cb2=findViewById(R.id.cb2);

cb3=findViewById(R.id.cb3);

cb1.setOnCheckedChangeListener(this);

//this指向MainActivity實(shí)例,該實(shí)例有OnCheckedChangeListener接口,

//當(dāng)事件觸發(fā),會(huì)自動(dòng)回調(diào)接口的onCheckedChanged()方法

cb2.setOnCheckedChangeListener(this);

cb3.setOnCheckedChangeListener(this);

}

@Override

publicvoidonCheckedChanged(CompoundButtonbuttonView,booleanisChecked){

//MainActivityOnCheckedChangeListener接口需要實(shí)現(xiàn)的方法

updateCheckBox();//updateCheckBox()同實(shí)現(xiàn)方法1

}

}2.6任務(wù)6-獲取并顯示EditText文本261EditText文本的獲取方法2EditText的hint屬性3Button點(diǎn)擊偵聽(tīng)實(shí)現(xiàn)布局my_main.xml27<LinearLayoutxmlns:android="/apk/res/android"

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent">

<TextView

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="YournameandID"/>

<EditText

android:id="@+id/editTextTextPersonName"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="輸入你的姓名"

android:ems="10"

android:inputType="textPersonName"

android:text=""/>

<Button

android:id="@+id/button"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="獲取編輯框文本"/>

<TextView

android:id="@+id/tv_result"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text=""/>

</LinearLayout>需要理解的屬性hint屬性,設(shè)置提示信息ems屬性,設(shè)置包圍內(nèi)容時(shí)的最小寬度inputType屬性,設(shè)置輸入類(lèi)型(包括輸入法)實(shí)現(xiàn)MainActivity28publicclassMainActivityextendsAppCompatActivity{

@Override

protectedvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.my_main);

EditTextet=findViewById(R.id.editTextTextPersonName);

TextViewtv=findViewById(R.id.tv_result);

findViewById(R.id.button).setOnClickListener(newView.OnClickListener(){

@Override

publicvoidonClick(Viewv){

tv.setText(et.getText().toString());

}

});

}

}2.7任務(wù)7-控制文本顏色以及UI邊距291內(nèi)邊距padding2外邊距margin3XML中顏色的表達(dá)4TextView控制字體顏色外邊距屬性舉例(1)android:layout_margin,設(shè)置UI上下左右外邊距(2)android:layout_marginStart,設(shè)置UI文字起始方向的外邊距(默認(rèn)是左邊距)(3)android:layout_marginEnd,設(shè)置UI文字結(jié)束方向的外邊距(默認(rèn)是右邊距)(4)android:layout_marginHorizontal,設(shè)置UI左右外邊距(5)android:layout_marginTop,設(shè)置UI的上外邊距(6)android:layout_marginBottom,設(shè)置UI的下外邊距

(7)android:layout_marginVertical,設(shè)置UI的上下外邊距內(nèi)邊距與外邊距的區(qū)別30XML中的顏色屬性值31#RGB用1位16進(jìn)制數(shù)分別表示紅色(R:Red)、綠色(G:Green)和藍(lán)色(B:Blue),值越大,對(duì)應(yīng)通道顏色值越大,例如#F00表示紅色。#RRGGBB用2位16進(jìn)制數(shù)表示對(duì)應(yīng)通道顏色,例如#F00000表示紅色通道值=0xF0h,其他通道顏色值為0的顏色,#F00與#FF0000等效。#ARGB在#RGB的基礎(chǔ)上增加了1位16進(jìn)制數(shù)表示Alpha通道,即透明度,值越大越不透明,若值為0則全透明。#AARRGGBB在#RRGGBB基礎(chǔ)上增加了2位16進(jìn)制數(shù)的Alpha通道,用于控制透明度屬性。res/values/color.xml32<resources>

<colorname="purple_200">#FFBB86FC</color>

<colorname="purple_500">#FF6200EE</color>

<colorname="purple_700">#FF3700B3</color>

<colorname="teal_200">#FF03DAC5</color>

<colorname="teal_700">#FF018786</color>

<colorname="black">#FF000000</color>

<colorname="white">#FFFFFFFF</color>

<colorname="other_color">#068E81</color>

</resources>注意,除了other_color以外,其他顏色是向?qū)Мa(chǎn)生的主題顏色XML中引用定義的顏色android:textColor="@color/other_color"Java代碼中使用定義的顏色getResources().getColor(R.color.other_color)Java代碼中預(yù)定義的顏色Color.RED,Color.BLUE等布局文件my_main.xml33<LinearLayoutxmlns:android="/apk/res/android"

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent">

<TextView

android:id="@+id/textView"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="YournameandID"/>

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

<Button

android:id="@+id/bt_red"

android:layout_width="0dp"

android:layout_weight="1"

android:layout_marginHorizontal="5dp"

android:layout_height="wrap_content"

android:text="Red"/>

<!--使用layout_marginHorizontal設(shè)置左右外邊距-->

<!--若使用padding屬性設(shè)置內(nèi)邊距無(wú)法使得Button之間隔開(kāi)空隙-->

<Button

android:id="@+id/bt_blue"

android:layout_width="0dp"

android:layout_weight="1"

android:layout_marginHorizontal="5dp"

android:layout_height="wrap_content"

android:text="Blue"/><Button

android:id="@+id/bt_other"

android:layout_width="0dp"

android:layout_weight="1"

android:layout_marginHorizontal="5dp"

android:layout_height="wrap_content"

android:text="Other"/>

</LinearLayout>

</LinearLayout>MainActivity.java34publicclassMainActivityextendsAppCompatActivity{

@Override

protectedvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.my_main);

TextViewtv=findViewById(R.id.textView);

findViewById(R.id.bt_red)

.setOnClickListener(newView.OnClickListener(){

@Override

publicvoidonClick(Viewv){

tv.setTextColor(Color.RED);

//TextView對(duì)象的setTextColor()方法可設(shè)置文本顏色

//Color類(lèi)是android提供的顏色類(lèi),預(yù)先定義了若干種常用顏色值

//Color.RED=0xFFFF0000,轉(zhuǎn)換回int值=-00010000h=-65536(補(bǔ)碼規(guī)則)

}

});

findViewById(R.id.bt_blue).setOnClickListener(

newView.OnClickListener(){

@Override

publicvoidonClick(Viewv){

//tv.setTextColor(Color.BLUE);

tv.setTextColor(0xff0000ff);//Color.BLUE的值,按#AARRGGBB解析

}

});

findViewById(R.id.bt_other).setOnClickListener(

newView.OnClickListener(){

@Override

publicvoidonClick(Viewv){

intotherColor=getResources().getColor(R.color.other_color);

//變量聲明可先使用方法調(diào)用,再利用Ctrl+Alt+V快捷鍵生成局部變量

tv.setTextColor(otherColor);

}

});

}

}2.8任務(wù)8-相對(duì)布局RelativeLayout351相對(duì)布局的定位方法2文本的基準(zhǔn)線對(duì)齊3樣式的引用相對(duì)布局的主要對(duì)齊方式UI相對(duì)RelativeLayout容器的位置關(guān)系或?qū)R關(guān)系UI相對(duì)容器位置關(guān)系的屬性名稱(chēng)與“parent”相關(guān),屬性值為true或falseandroid:layout_centerInParentandroid:layout_alignParentRightandroid:layout_alignParentBottomUI相對(duì)其他UI組件的位置關(guān)系屬性值為參考對(duì)象的UI的id引用android:layout_below,android:layout_aboveandroid:layout_toLeftOf,android:layout_toRightOfandroid:layout_toStartOf,android:layout_toEndOfUI相對(duì)其他UI基準(zhǔn)線的位置對(duì)齊android:layout_alignBottomUI的底部與參考UI的底部對(duì)齊android:layout_alignBaseline文本基準(zhǔn)線對(duì)齊36UI底部對(duì)齊和文本基準(zhǔn)線對(duì)齊的區(qū)別37<TextView

android:id="@+id/tv1"

android:layout_width="wrap_c

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論