1? ? ? 基本UI
Zealer、CSDN、github
StormZhang、
張弘揚(yáng)(Hyman):http://blog.csdn.net/lmj623565791?viewmode=contents
、任玉剛:http://blog.csdn.net/singwhatiwanna/article/details/38426471
、徐宜生
Android官方文檔(最好的)
Android中文API:http://www.android-doc.com/
Android的開(kāi)發(fā)者網(wǎng)站:http://developer.android.com/index.html
Android參考書(shū)
第一行代碼、Android瘋狂講義
推薦有經(jīng)驗(yàn)的人看的書(shū):和源碼有關(guān)的,設(shè)計(jì)模式,Thinking InJava(Java編程思想)
www.javaapk.com
Android的SDK中定義了一個(gè)View類(lèi),它是所有Android控件和容器的父類(lèi)?!禫iew和ViewGroup》。
View和ViewGroup的概念
View:Android中所有控件的頂層基類(lèi)
ViewGroup:是View的子類(lèi),代表一個(gè)View的容器,可以用于存放其他View對(duì)象
布局類(lèi)(就是ViewGroup類(lèi)的子類(lèi))
不同的布局管理內(nèi)部空間的方式不一樣,在這里我們先簡(jiǎn)單認(rèn)識(shí)一個(gè)布局,就是線性布局
線性布局(LinearLayout):是按照線性排列(橫向、縱向)的方式對(duì)內(nèi)部控件的位置進(jìn)行管理
常用屬性:android:orientation="vertical"[ horizontal]
View類(lèi)的常用屬性:
【
android:id設(shè)置控件的標(biāo)識(shí)符號(hào)
android:layout_width設(shè)置子組件的布局寬度
android:layout_height設(shè)置子組件的布局高度
android:background設(shè)置控件的背景色
android:onClick設(shè)置控件的單擊事件綁定監(jiān)聽(tīng)器
android:visibility設(shè)置控件是否可見(jiàn)(invisible/visible/gone<不可見(jiàn),不占位>
tv_name.setVisibility(View.VISIBLE);)
android:alpha設(shè)置控件透明度(0-1之間的數(shù)值)
android:padding設(shè)置子組件的內(nèi)邊距
android:layout_margin設(shè)置子組件的外邊距
android:onClick為控件設(shè)置點(diǎn)擊事件對(duì)應(yīng)的回調(diào)方法
】
android:layout_width
android:layout_height
屬性值:1)具體的長(zhǎng)度?? 2)wap_content, 包裹內(nèi)容(根據(jù)內(nèi)容的大小調(diào)整控件的大?。? 3)fill_parent:填充父控件(從Android2.1開(kāi)始被match_parent替代)??????? 4)match_parent:填充父控件
PS:以上兩個(gè)屬性是任何一個(gè)控件都必須要設(shè)置的,并且長(zhǎng)度單位推薦使用:dp常用的基本控件:
TextView:基本文本標(biāo)簽,用于展示一個(gè)簡(jiǎn)單文本
常用屬性:
android:text:文本的內(nèi)容
android:textSize:文字的大小
android:textColor:文本的顏色《#RGB
#ARGB
#RRGGBB
#AARRGGBB
android:textColor="@android:color/holo_green_light"
》????????? android:gravity:控制控件內(nèi)容的位置
android:drawableXXX:在控件中放置圖片
android:autoLink:設(shè)置超鏈接
需要了解的屬性:
【
android:shadowDx -------陰影的橫坐標(biāo)偏移
android:shadowDy-------陰影的縱坐標(biāo)偏移
android:shadowRadius--------陰影的半徑
android:shadowColor---------陰影的顏色
android:textAllCaps-------所有字母大寫(xiě)
android:password? ------------將文字設(shè)置為密文
】
代碼操作法:
【
//在代碼中修改TextView的文本內(nèi)容
//根據(jù)id在當(dāng)前的頁(yè)面中尋找控件
tv_name = (TextView)this.findViewById(R.id.tv_name);
//設(shè)置文本的內(nèi)容,大多數(shù)情況下,一個(gè)控件在布局中設(shè)置的屬性都有對(duì)應(yīng)的方法可以在代碼中去設(shè)置
tv_name.setText("修改了,文件的文本!");
//?????????????? tv_name.setTextColor(Color.BLUE);
tv_name.setTextColor(Color.parseColor("#FF0000"));
tv_name.setTextSize(30);
】
1.3.1? 使用TextView實(shí)現(xiàn)跑馬燈效果
需要設(shè)置以下屬性:
1)??????android:singleLine="true"
2)??????android:ellipsize="marquee"??? //跑馬燈效果
3)??????android:focusable="true"?????//讓控件可獲取焦點(diǎn)
4)??????android:focusableInTouchMode="true"???????? // 讓控件在觸摸模式下獲取焦點(diǎn)
5)??????可選的屬性:android:marqueeRepeatLimit//文本滾動(dòng)的次數(shù)
屬性值:-1或marquee_forever,代表無(wú)限滾動(dòng)
具體的滾動(dòng)次數(shù)(比如設(shè)置為2)
使用跑馬燈的前提:1)文本內(nèi)容必須要超出控件的寬度?????? 2)必須單行顯示
android:singleLine="true"
android:maxEms="8"
android:ellipsize="end"
android:singleLine="true"
android:ellipsize="end"
android:ems="15"
設(shè)置TextView的行間距、行高。
【
1、android:lineSpacingExtra
設(shè)置行間距,如”8dp”。
2、android:lineSpacingMultiplier
設(shè)置行間距的倍數(shù),如”1.5″。
】
設(shè)置每行的最大字?jǐn)?shù)
【
maxLength和固定15個(gè)字的寬度或代碼控制加\n
android:singleLine="false".
android:maxEms
setText(Html.fromHtml()
關(guān)于文本顏色的操作代碼設(shè)置:
private voidsetText(TextView t){
String text =t.getText().toString().trim();
SpannableStringspan =newSpannableString(text);
span.setSpan(newForegroundColorSpan(Color.WHITE),0,1,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
t.setText(span);
}
tv_lingqu_tishi.setTextColor(Color.parseColor("#ffffff"));
】
繼承自TextView,具有TextView的所有屬性
【
注冊(cè)監(jiān)聽(tīng)事件的四種方式:
1、 設(shè)置 android:onClick 屬性
《android:onClick="onBtnClick"
// 當(dāng)按鈕被點(diǎn)擊的時(shí)候調(diào)用的方法
publicvoid onBtnClick(View v) {
System.out.println("使用onClick屬性");
Toast.makeText(MainActivity.this,"使用onClick屬性", 1).show();
}
》
2、 成員內(nèi)部類(lèi)
《//方式二:使用成員內(nèi)部類(lèi)
privateclass MyListener implements OnClickListener{
@Override
publicvoid onClick(View v) {
//TODO Auto-generated method stub
System.out.println("方式二:使用成員內(nèi)部類(lèi)");
Toast.makeText(MainActivity.this,"方式二:使用成員內(nèi)部類(lèi)", 1).show();
}
}
button2.setOnClickListener(newMyListener());
》
3、 匿名內(nèi)部類(lèi)
//方式三:使用匿名內(nèi)部類(lèi)
button3= (Button) this.findViewById(R.id.button3);
button3.setOnClickListener(newView.OnClickListener() {
@Override
publicvoid onClick(View v) {
System.out.println("方式三:使用匿名內(nèi)部類(lèi)");
Toast.makeText(MainActivity.this,"方式三:使用匿名內(nèi)部類(lèi)", 1).show();
}
});
4、 當(dāng)前Activity實(shí)現(xiàn)監(jiān)聽(tīng)接口(OnClickListener)
《public class MainActivity extends Activity implementsOnClickListener {
//方式四:實(shí)現(xiàn)監(jiān)聽(tīng)接口
@Override
publicvoid onClick(View v) {
//TODO Auto-generated method stub
switch(v.getId()) {
caseR.id.button4:
System.out.println("方式四:實(shí)現(xiàn)監(jiān)聽(tīng)接口1");
Toast.makeText(MainActivity.this,"方式四:實(shí)現(xiàn)監(jiān)聽(tīng)接口1", 1).show();
break;
caseR.id.button5:
System.out.println("方式四:實(shí)現(xiàn)監(jiān)聽(tīng)接口2");
Toast.makeText(MainActivity.this,"方式四:實(shí)現(xiàn)監(jiān)聽(tīng)接口2", 1).show();
break;
default:
break;
}
}
}》
】
【ems
--EditText類(lèi)繼承自TextView類(lèi),具有TextView的所有屬性
--EditText與TextView最大的不同就是用戶可以對(duì)EditText控件進(jìn)行編輯,同時(shí)還可以為EditText控件設(shè)置監(jiān)聽(tīng)器,用來(lái)判斷用戶的輸入是否合法
android:hint輸入提示
android:textColorHint提示的顏色
android:textColorHighlight選中字體的背景顏色
android:inputType輸入類(lèi)型的可選項(xiàng)
監(jiān)聽(tīng)輸入框中文本內(nèi)容的變化:addTextChangedListener(TextWatcher watcher)
】
《//監(jiān)聽(tīng)EditText中文本變化
name.addTextChangedListener(newMyWather());
//監(jiān)聽(tīng)EditText中文本變化
classMyWather implements TextWatcher{
@Override
publicvoid onTextChanged(CharSequence s,int start, int before,
int count) {
//TODO Auto-generated method stub
System.out.println("文本發(fā)生改變....."+s);
}
}
》
if(TextUtils.isEmpty(namea) || TextUtils.isEmpty(pwd))
【
繼承自CompoundButton(帶選擇狀態(tài)的按鈕)
CheckBox繼承于Button,所以具有普通按鈕的各種屬性,但是與普通按鈕不同的是,CheckBox 提供了可選中的功能。
《
private class MyListener? implements OnCheckedChangeListener{
@Override
publicvoid onCheckedChanged(CompoundButton buttonView,
booleanisChecked) {
switch(buttonView.getId()) {
caseR.id.cbox_color:
if(isChecked){
tv.setTextColor(Color.RED);
}else{
tv.setTextColor(Color.BLACK);
}
break;
}
}
cbox_color.setOnCheckedChangeListener(newMyListener());
》
】
練習(xí)
【
publicclass MainActivity extends Activity implements OnCheckedChangeListener {
private TextView show_text;
private CheckBox cb1, cb2, cb3, cb_all;
@Override
protected void onCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViews();
initListener();
}
private void initListener() {
cb1.setOnCheckedChangeListener(this);
cb2.setOnCheckedChangeListener(this);
cb3.setOnCheckedChangeListener(this);
//為了避免選中狀態(tài)處理的沖突,我們只需要考慮點(diǎn)擊“全選”按鈕時(shí)的業(yè)務(wù)邏輯
cb_all.setOnClickListener(newOnClickListener() {
@Override
public voidonClick(View v) {
boolean checked = cb_all.isChecked();
cb1.setChecked(checked);
cb2.setChecked(checked);
cb3.setChecked(checked);
}
});
}
private void findViews() {
show_text = (TextView)this.findViewById(R.id.show_text);
cb1 = (CheckBox)this.findViewById(R.id.cb1);
cb2 = (CheckBox)this.findViewById(R.id.cb2);
cb3 = (CheckBox)this.findViewById(R.id.cb3);
cb_all = (CheckBox)this.findViewById(R.id.cb_all);
}
@Override
public voidonCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//根據(jù)當(dāng)前三個(gè)復(fù)選框的選中狀態(tài)修改“全選/取消全選”的選中狀態(tài)
if(cb1.isChecked()&&cb2.isChecked()&&cb3.isChecked()){
cb_all.setChecked(true);
}else{
cb_all.setChecked(false);
}
updateText();
}
private void updateText() {
//
show_text.setText("");
//逐個(gè)判斷三個(gè)復(fù)選框的選中狀態(tài),顯示被選中的
if(cb1.isChecked()){
//??????????????????????? show_text.setText(cb1.getText()+"");
show_text.append(cb1.getText()+"");
}
if(cb2.isChecked()){
//??????????????????????? show_text.setText(cb2.getText()+"");
show_text.append(cb2.getText()+" ");
}
if(cb3.isChecked()){
//??????????????????????? show_text.setText(cb3.getText()+"");
show_text.append(cb3.getText()+"");
}
}
}
】
Activity與用戶交互的界面
Service后臺(tái)服務(wù)
BroadcastReceiver廣播接收器
ContentProvider內(nèi)容提供者(可以通過(guò)該組件實(shí)現(xiàn)不同進(jìn)程間的數(shù)據(jù)交互)
1.8????RadioButton、RadioGroup 單選按鈕(組)
【
==RadioButton繼承于Button,所以具有普通按鈕的各種屬性,但是與普通按鈕不同的是,在使用RadioButton的時(shí)候,要使用RadioGroup來(lái)包圍起RadioButton
==RadioGroup是LinearLayout的子類(lèi),所以RadioGroup本質(zhì)上是一個(gè)存放RadioButton的布局容器
==默認(rèn)的LinearLayout布局的Orientation屬性是水平的,而默認(rèn)的RadioGroup的Orientation屬性是垂直的
==RadioGroup類(lèi)中的getCheckedRadioButtonId()根據(jù)id獲得RadioGroup里指定的RadioButton
示例說(shuō)明:
radio_group= (RadioGroup)this.findViewById(R.id.radio_group);
radio_group.setOnCheckedChangeListener(newOnCheckedChangeListener() {
/**
*當(dāng)RadioGroup中的單選按鈕被選擇時(shí)調(diào)用的方法
*
*/
@Override
publicvoidonCheckedChanged(RadioGroup group,intcheckedId) {
switch(checkedId) {
caseR.id.ra_male://男
Toast.makeText(MainActivity.this,"你的選擇是,男",0).show();
break;
caseR.id.ra_female://女
Toast.makeText(MainActivity.this,"你的選擇是,女",0).show();
break;
default:
break;
}
}
});
switch(radio_group.getCheckedRadioButtonId()){
caseR.id.ra_male:
weiht= (in_height-80)*0.7;
break;
caseR.id.ra_female:
weiht= (in_height-70)*0.6;
break;
default:
break;
}
】
【
android:id="@+id/radio_data"
android:drawableTop="@drawable/slt_data"
android:text="DATA"
style="@style/radiobutton_style"/>
0dp
wrap_content
1
@null
@null
center
@color/select_text_color
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/radio_select"
android:clickable="false"
android:checked="true"/>
@Override
publicvoidonPageSelected(intpotion) {
//TODOAuto-generated method stub
//獲取應(yīng)該被選中的radiobutton對(duì)象,getChildAt方法:獲取radiogroup中位于position位置的子控件對(duì)象
RadioButton rb =(RadioButton)rGroup.getChildAt(potion);
//設(shè)置讓參數(shù)中指定的id對(duì)應(yīng)RadioButton對(duì)象變?yōu)檫x中效果
rGroup.check(rb.getId());
}
】
【
繼承自View
andorid:src設(shè)置圖片來(lái)源
android:adjustViewBounds用于設(shè)置ImageView 是否調(diào)整自己的邊界,來(lái)保持所顯示圖片的長(zhǎng)寬比例
android:maxHeight設(shè)置 ImageView 的最大高度
《需要先設(shè)置android:adjustViewBounds為true,否則不起作用》
andorid:maxWidth設(shè)置 ImageView 的最大寬度
android:scaleType設(shè)置所顯示的圖片如何縮放或移動(dòng),以適應(yīng)ImageView的大小。
setImageBitmap()設(shè)置Bitmap作為圖片源
setImageDrawable()設(shè)置Drawable作為圖片源
setImageResource()設(shè)置Resource作為圖片源
《
//方式一:Drawable類(lèi)描述的是資源文件中的圖像資源
//Drawabledrawable = getResources().getDrawable(R.drawable.hjgh_n);
//iv.setImageDrawable(drawable);
//方式二:在Android中是用Bitmap類(lèi)來(lái)描述圖片
//通過(guò)BitmapFactory(位圖工廠)生成Bitmap對(duì)象
/* Bitmap bm =BitmapFactory.decodeResource(getResources(),R.drawable.hjgh_n);
iv.setImageBitmap(bm);*/
//方式三:
iv.setImageResource(R.drawable.hjgh_n);
》
獲取資源文件實(shí)現(xiàn)切換效果:
《
private int[] imgs ={R.drawable.lz1,R.drawable.lz2,R.drawable.lz3};
int index = 0;
public void xiaClick(View v){
if(index ==(imgs.length-1))?????? {
index=0;
}else{
index++;
}
imView.setImageResource(imgs[index]);
}
》
setImageDrawable(drawable);
setImageBitmap(bm);
setImageResource(resId);
怎么在代碼中設(shè)ImageView的background屬性
setBackgroundResource(R.drawable.down_finish);
//???????????????????iv_startguid.setImageDrawable(getResources().getDrawable(R.drawable.btn_start_guide));
iv_startguid.setBackgroundResource(R.drawable.btn_start_guide);
】
1、先看是什么異常(首先找caused by)
3、自上而下查找第一行自己寫(xiě)的代碼
2、再看異常的描述信息
1、資源文件命名錯(cuò)誤:文件名只能包含小寫(xiě)字母、數(shù)字和下劃線,并且不能以數(shù)字開(kāi)頭
2、資源文件夾命名錯(cuò)誤:文件夾名只能用Android定義好的
1、應(yīng)用程序包名出錯(cuò)(比如:包含中文)
2、誤刪除
設(shè)備斷開(kāi)連接(連接超時(shí))