//顯示文字 Toast.makeText(this,"Toast顯示文本",Toast.LENGTH_SHORT).show(); //顯示圖片 Toast toast = new Toast(this); ImageView imageView = new ImageView(this); imageView.setImageResource(R.mipmap.ic_launcher); toast.setView(imageView); toast.show();
//圖文 Toast toast = new Toast(this); TextView textView = new TextView(this); textView.setText("顯示帶圖片的toast"); ImageView imageView = new ImageView(this); //設置圖片 imageView.setImageResource(R.mipmap.ic_launcher); //得到Toast的佈局 LinearLayout layout = new LinearLayout(this); //設置此佈局爲橫向的 layout.setOrientation(LinearLayout.VERTICAL); //將ImageView再加入到此佈局中的第一個位置 layout.addView(imageView); //將TextView放到第二個位置 layout.addView(textView); toast.setView(layout); toast.setGravity(Gravity.CENTER,0,0); toast.setDuration(Toast.LENGTH_SHORT); toast.show();
/** * TextView * 向用戶顯示文本和選擇性的容許編輯他。一個TextView是一個完整的文本編輯器,可是基本的類配置爲 * 不容許編輯;容許用戶賦值TextView價值部分或前部粘貼到其餘地方,設置XML屬性 * android:textIsSelectable爲true或者叫textView.setTextIsSelectable(true)。 * textIsSelectable標記TextView容許用戶作出選擇的手勢,進而觸發系統內置的複製粘貼控制 */
TextView屬性大全:android
android:autoLink設置是否當文本爲URL連接/email/電話號碼/map時,文本顯示爲可點擊的連接。可選值(none/web/email/phone/map/all) android:autoText若是設置,將自動執行輸入值的拼寫糾正。此處無效果,在顯示輸入法並輸入的時候起做用。 android:bufferType指定getText()方式取得的文本類別。選項editable 相似於StringBuilder可追加字符, 也就是說getText後可調用append方法設置文本內容。spannable 則可在給定的字符區域使用樣式 android:capitalize設置英文字母大寫類型。此處無效果,須要彈出輸入法才能看獲得,參見EditView此屬性說明。 android:cursorVisible設定光標爲顯示/隱藏,默認顯示。 android:digits設置容許輸入哪些字符。如「1234567890.+-*/% ()」 android:drawableBottom在text的下方輸出一個drawable,如圖片。若是指定一個顏色的話會把text的背景設爲該顏色,而且同時和background使用時覆蓋後者。 android:drawableLeft在text的左邊輸出一個drawable,如圖片。 android:drawablePadding設置text與drawable(圖片)的間隔,與drawableLeft、drawableRight、drawableTop、drawableBottom一塊兒使用,可設置爲負數,單獨使用沒有效果。 android:drawableRight在text的右邊輸出一個drawable。 android:drawableTop在text的正上方輸出一個drawable。 android:editable設置是否可編輯。 android:editorExtras設置文本的額外的輸入數據。 android:ellipsize設置當文字過長時,該控件該如何顯示。有以下值設置:」start」—?省略號顯示在開頭;」end」——省略號顯示在結尾;」middle」—-省略號顯示在中間; 」marquee」 ——以跑馬燈的方式顯示(動畫橫向移動) android:freezesText設置保存文本的內容以及光標的位置。 android:gravity設置文本位置,如設置成「center」,文本將居中顯示。 android:hintText爲空時顯示的文字提示信息,可經過textColorHint設置提示信息的顏色。此屬性在EditView中使用,可是這裏也能夠用。 android:imeOptions附加功能,設置右下角IME動做與編輯框相關的動做,如actionDone右下角將顯示一個「完成」,而不設置默認是一個回車符號。這個在EditView中再詳細 說明,此處無用。 android:imeActionId設置IME動做ID。 android:imeActionLabel設置IME動做標籤。 android:includeFontPadding設置文本是否包含頂部和底部額外空白,默認爲true。 android:inputMethod爲文本指定輸入法,須要徹底限定名(完整的包名)。例如:com.google.android.inputmethod.pinyin,可是這裏報錯找不到。 android:inputType設置文本的類型,用於幫助輸入法顯示合適的鍵盤類型。在EditView中再詳細說明,這裏無效果。 android:linksClickable設置連接是否點擊鏈接,即便設置了autoLink。 android:marqueeRepeatLimit在ellipsize指定marquee的狀況下,設置重複滾動的次數,當設置爲marquee_forever時表示無限次。 android:ems設置TextView的寬度爲N個字符的寬度。這裏測試爲一個漢字字符寬度 android:maxEms設置TextView的寬度爲最長爲N個字符的寬度。與ems同時使用時覆蓋ems選項。 android:minEms設置TextView的寬度爲最短爲N個字符的寬度。與ems同時使用時覆蓋ems選項。 android:maxLength限制顯示的文本長度,超出部分不顯示。 android:lines設置文本的行數,設置兩行就顯示兩行,即便第二行沒有數據。 android:maxLines設置文本的最大顯示行數,與width或者layout_width結合使用,超出部分自動換行,超出行數將不顯示。 android:minLines設置文本的最小行數,與lines相似。 android:lineSpacingExtra設置行間距。 android:lineSpacingMultiplier設置行間距的倍數。如」1.2」 android:numeric若是被設置,該TextView有一個數字輸入法。此處無用,設置後惟一效果是TextView有點擊效果,此屬性在EdtiView將詳細說明。 android:password以小點」.」顯示文本 android:phoneNumber設置爲電話號碼的輸入方式。 android:privateImeOptions設置輸入法選項,此處無用,在EditText將進一步討論。 android:scrollHorizontally設置文本超出TextView的寬度的狀況下,是否出現橫拉條。 android:selectAllOnFocus若是文本是可選擇的,讓他獲取焦點而不是將光標移動爲文本的開始位置或者末尾位置。TextView中設置後無效果。 android:shadowColor指定文本陰影的顏色,須要與shadowRadius一塊兒使用。 android:shadowDx設置陰影橫向座標開始位置。 android:shadowDy設置陰影縱向座標開始位置。 android:shadowRadius設置陰影的半徑。設置爲0.1就變成字體的顏色了,通常設置爲3.0的效果比較好。 android:singleLine設置單行顯示。若是和layout_width一塊兒使用,當文本不能所有顯示時,後面用「…」來表示。如android:text="test_ singleLine " android:singleLine="true" android:layout_width="20dp"將只顯示「t…」。若是不設置singleLine或者設置爲false,文本將自動換行 android:text設置顯示文本. android:textAppearance設置文字外觀。如「?android:attr/textAppearanceLargeInverse」這裏引用的是系統自帶的一個外觀,?表示系統是否有這種外觀,不然使用默認的 外觀。可設置的值以下: textAppearanceButton/textAppearanceInverse/textAppearanceLarge/textAppearanceLargeInverse/textAppearanceMedium/textAppearanceMediumInverse/textAppearanceSmal l/textAppearanceSmallInverse android:textColor設置文本顏色 android:textColorHighlight被選中文字的底色,默認爲藍色 android:textColorHint設置提示信息文字的顏色,默認爲灰色。與hint一塊兒使用。 android:textColorLink文字連接的顏色. android:textScaleX設置文字之間間隔,默認爲1.0f。 android:textSize設置文字大小,推薦度量單位」sp」,如」15sp」 android:textStyle設置字形[bold(粗體) 0, italic(斜體) 1, bolditalic(又粗又斜) 2] 能夠設置一個或多個,用「|」隔開 android:typeface設置文本字體,必須是如下常量值之一:normal 0, sans 1, serif 2, monospace(等寬字體) 3] android:height設置文本區域的高度,支持度量單位:px(像素)/dp/sp/in/mm(毫米) android:maxHeight設置文本區域的最大高度 android:minHeight設置文本區域的最小高度 android:width設置文本區域的寬度,支持度量單位:px(像素)/dp/sp/in/mm(毫米),與layout_width的區別看這裏。 android:maxWidth設置文本區域的最大寬度 android:minWidth設置文本區域的最小寬度
1、EditText輸入的文字爲密碼形式的設置 (1)經過.xml裏設置: 把該EditText設爲:android:password="true" // 以」.」形式顯示文本 (2)在代碼裏設置: 經過設置EditText的setTransformationMethod()方法來實現隱藏密碼或這顯示密碼。 editText.setTransformationMethod(PasswordTransformationMethod.getInstance());//設置密碼爲不可見。 二、(1)EditText輸入的文字爲電話號碼 Android:phoneNumber=」true」 //輸入電話號碼 3、EditText字數限制的設置 (1)在.xml中設置:android:maxLength=「50」 (2)代碼中設置: editText.setFilters(new InputFilter[]{newInputFilter.LengthFilter(100)}); 4、EditText設置字體 android:typeface="monospace" //設置字型。字形有:normal, sans, serif,monospace 5、EditText是否可編輯 Android:editable // 是否可編輯 6、在EditText中軟鍵盤的調起、關閉 (1)EditText有焦點(focusable爲true)阻止輸入法彈出 editText=(EditText)findViewById(R.id.txtBody); editText.setOnTouchListener(new OnTouchListener(){ public boolean onTouch(View v, MotionEvent event){ editText.setInputType(InputType.TYPE_NULL); //關閉軟鍵盤 return false; } }); (2)當EidtText無焦點(focusable=false)時阻止輸入法彈出 InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(editText.getWindowToken(),0); (3)調用數字鍵盤並設置輸入類型和鍵盤爲英文 etNumber.setInputType(InputType.TYPE_CLASS_NUMBER); //調用數字鍵盤 rlEditText.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);//設置輸入類型和鍵盤爲英文 或者:android:inputType="textUri|textMultiLine" (4)android:focusable="false"//鍵盤永遠不會彈出 <activity android:name=".AddLinkman"android:windowSoftInputMode="adjustUnspecified|stateHidden"/>//不自動彈出鍵盤 //關閉鍵盤(好比輸入結束後執行) InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(etEditText.getWindowToken(), 0); //自動彈出鍵盤 ((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).toggleSoftInput(0,InputMethodManager.HIDE_NOT_ALWAYS); etEditText.requestFocus();//讓EditText得到焦點,可是得到焦點並不會自動彈出鍵盤 7、android:layout_gravity和android:gravity的區別 (1)android:layout_gravity是本元素對父元素的重力方向。 (2)android:gravity是本元素全部子元素的重力方向。 8、android:padding和android:layout_margin區別 這兩個均可以設置邊距,但有細微的區別: (1)android:padding是相對父view的邊距 (2)android:layout_margin是相對同一級View的邊距 例:LinearLayout是水平佈局,下面有兩個按鈕, (a)若是右邊的按鈕想距左邊的按鈕15px,由於這兩個按鈕是同一級的,應該用android:layout_margin; (b)若是右邊的按鈕想距左邊的距離爲350px,應該用android:padding 九、android:numeric//只接受數字 android:numeric來控制輸入的數字類型,一共有三種分別爲integer(正整數)、signed(帶符號整數,有正負)和decimal(浮點數)。 10、Enter鍵圖標的設置 軟鍵盤的Enter鍵默認顯示的是「完成」文本,咱們知道按Enter建表示前置工做已經準備完畢了,要去什麼什麼啦。好比,在一個搜索中,咱們輸入要搜索的文本,而後按Enter表示要去搜索了,可是默認的Enter鍵顯示的是「完成」文本,看着不太合適,不符合搜索的語義,若是能顯示「搜索」兩個字或者顯示一個表示搜索的圖標多好。事實證實咱們的想法是合理的,Android也爲咱們提供的這樣的功能。經過設置android:imeOptions來改變默認的「完成」文本。這裏舉幾個經常使用的常量值: (1)actionUnspecified未指定,對應常量EditorInfo.IME_ACTION_UNSPECIFIED效果: (2)actionNone 沒有動做,對應常量EditorInfo.IME_ACTION_NONE效果: (3)actionGo去往,對應常量EditorInfo.IME_ACTION_GO 效果: (4)actionSearch 搜索,對應常量EditorInfo.IME_ACTION_SEARCH效果: (5)actionSend 發送,對應常量EditorInfo.IME_ACTION_SEND效果: (6)actionNext 下一個,對應常量EditorInfo.IME_ACTION_NEXT效果: (7)actionDone 完成,對應常量EditorInfo.IME_ACTION_DONE效果: 11、使用android:imeOptinos可對Android自帶的軟鍵盤進行一些界面上的設置: android:imeOptions="flagNoExtractUi" //使軟鍵盤不全屏顯示,只佔用一部分屏幕 同時,這個屬性還能控件軟鍵盤右下角按鍵的顯示內容,默認狀況下爲回車鍵 android:imeOptions="actionNone" //輸入框右側不帶任何提示 android:imeOptions="actionGo" //右下角按鍵內容爲'開始' android:imeOptions="actionSearch" //右下角按鍵爲放大鏡圖片,搜索 android:imeOptions="actionSend" //右下角按鍵內容爲'發送' android:imeOptions="actionNext" //右下角按鍵內容爲'下一步' android:imeOptions="actionDone" //右下角按鍵內容爲'完成' 12、限定edittext能輸入數字和字母,而且默認輸入爲數字,如身份證號碼 android:inputType="number" android:digits="0123456789xyzXYZ" 13、軟鍵盤的調起致使原來的界面被擠上去,或者致使界面下面的tab導航被擠上去,解決方法以下 解決方法: 使用Manifest中的Activity的android:windowSoftInputMode的"adjustPan"屬性。 另外注意:有關軟鍵盤的問題可參考android:windowSoftInputMode中屬性。 1四、edittext光標詳解 edittext.requestFocusFromTouch();//讓光標放入到點擊位置。 edittext.requestFocus();//默認方式得到焦點 EditText editor = (EditText)getCurrentView();//光標處插入 int cursor = editor.getSelectionStart(); editor.getText().insert(cursor,delta); 讓光標移到末端(這樣文字就會向前顯示) EditText et = ... String text = "text"; et.setText(text); et.setSelection(text.length()); android:cursorVisible="false" 隱藏光標 android:background="#00000000"//不要文本框背景 Android - 文本框的輸入法控制和默認焦點設置 在開發中,必不可少的會使用到文本框(EditText)來進行數據錄入,也就會須要對輸入法進行一些控制。 先看下LAYOUT定義文件中的和輸入法有關的屬性: 屬性名 說明 android:inputType 指定輸入法的類型,int類型,能夠用|選擇多個。取值能夠參考:android.text.InputType類。取值包括:text, textUri, phone,number,等。 android:imeOptions 指定輸入法窗口中的回車鍵的功能,可選值爲normal, actionNext,actionDone,actionSearch等。部分輸入法對此的支持可能不夠好。 下面的LAYOUT定義文件舉了一些例子說明inputType和imeOptions的使用。 <EditText android:id="@+id/textNormal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="Normal text" android:inputType="text" android:imeOptions="actionNext" /> <EditText android:id="@+id/textInteger" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="Integer only" android:inputType="number" android:imeOptions="actionNext" /> <EditText android:id="@+id/textPhone" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="Phone number" android:inputType="phone" android:imeOptions="actionNext" /> <EditText android:id="@+id/textEmail" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="Email" android:imeOptions="actionSend" android:inputType="textEmailAddress" /> <EditText android:id="@+id/textSite" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="Web Site" android:imeOptions="actionDone" android:inputType="textUri" /> 有時候也要對intent的默認焦點進行設置,不至於在intent跳轉的時候默認焦點(光標)在EditText上,致使進入intent就打開輸入法,影響界面美觀。 默認焦點的順序是:從上倒下 從左到右第一個能夠輸入的控件做爲焦點 可使用: button.setFocusable(true); button.requestFocus(); button.setFocusableInTouchMode(true); 也能夠: 在EditText前面放置一個看不到的LinearLayout,讓他率先獲取焦點: <LinearLayout android:focusable="true" android:focusableInTouchMode="true" android:layout_width="0px" android:layout_height="0px"/> Android EditText 屬性彙總 Android EditText 屬性彙總 android:layout_gravity="center_vertical" 設置控件顯示的位置:默認top,這裏居中顯示,還有bottom android:hint="請輸入數字!" 設置顯示在空間上的提示信息 android:numeric="integer" 設置只能輸入整數,若是是小數則是: decimal android:singleLine="true" 設置單行輸入,一旦設置爲true,則文字不會自動換行。 android:password="true" 設置只能輸入密碼 android:textColor = "#ff8c00" 字體顏色 android:textStyle="bold" 字體,bold, italic, bolditalic android:textSize="20dip" 大小 android:capitalize = "characters" 以大寫字母寫 android:textAlign="center" EditText沒有這個屬性,但TextView有 android:textColorHighlight="#cccccc" 被選中文字的底色,默認爲藍色 android:textColorHint="#ffff00" 設置提示信息文字的顏色,默認爲灰色 android:textScaleX="1.5" 控制字與字之間的間距 android:typeface="monospace" 字型,normal, sans, serif, monospace android:background="@null" 空間背景,這裏沒有,指透明 android:layout_weight="1" 權重,控制控件之間的地位,在控制控件顯示的大小時蠻有用的。 android:textAppearance="?android:attr/textAppearanceLargeInverse" 文字外觀,這裏引用的是系統自帶的一個外觀,?表示系統是否有這種外觀,不然使用默認的外觀。不知道這樣理解對不對? 經過EditText的layout xml文件中的相關屬性來實現: 1. 密碼框屬性 android:password="true" 這條可讓EditText顯示的內容自動爲星號,輸入時內容會在1秒內變成*字樣。 2. 純數字 android:numeric="true" 這條可讓輸入法自動變爲數字輸入鍵盤,同時僅容許0-9的數字輸入 3. 僅容許 android:capitalize="cwj1987" 這樣僅容許接受輸入cwj1987,通常用於密碼驗證 下面是一些擴展的風格屬性 android:editable="false" 設置EditText不可編輯 android:singleLine="true" 強制輸入的內容在單行 android:ellipsize="end" 自動隱藏尾部溢出數據,通常用於文字內容過長一行沒法所有顯示時
/** * Button * 1.經常使用屬性 * 二、OnClickListener事件 * (1)本類實現View.OnClickListener接口 * (2)使用內部類 * (3)自定義方法,配置android:onclick屬性 * 三、設置透明的Button * style="?android:attr/borderlessButtonStyle" * 四、配置Button style * 經過配置android:background來設置按鈕樣式 * 使用XML drawable selector * (1)首先在drawable中新建一個drawable resource file命名爲bg_button(即selector) * 在裏面設置按鈕點擊與沒被點擊時的背景 * (2)在xml裏經過background設置 * android:background="@drawable/bg_button"便可 */
Button點擊事件:git
方法一:web
<Button android:id="@+id/btn1" android:onClick="Select" android:text="Select" android:layout_width="wrap_content" android:layout_height="wrap_content" />
public void Select(View v) { Toast.makeText(this,"點擊事件",Toast.LENGTH_SHORT).show(); }
方法二(使用內部類的方式):api
private Button btn1; btn1 = (Button) findViewById(R.id.btn1); btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(MainActivity.this,"點擊事件",Toast.LENGTH_SHORT).show(); } });
Button設置樣式app
bg_button.xml框架
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@color/colorAccent" android:state_pressed="true"></item> <item android:drawable="@color/colorPrimary" android:state_pressed="false"></item> </selector>
<Button android:id="@+id/btn1" android:onClick="Select" android:text="Select" android:background="@drawable/bg_button" android:layout_width="wrap_content" android:layout_height="wrap_content" />
/** * ImageView * XML屬性 * android:src:設置View的drawable(如圖片,也能夠是顏色,可是須要指定View的大小) * android:adjustViewBounds:是否保持寬高比。須要與maxWidth、maxHeight一塊兒使用,單獨使用沒有效果 * android:maxHeight:設置View的最大高度,單獨使用無效,須臾setAdjustViewBounds一塊兒使用 * 若是想設置圖片固定大小,郵箱保持圖片寬高比,須要以下設置: * (1)設置setAdjustViewBounds爲true * (2)設置maxHeight,maxWidth * (3)設置layout_width和layout_height爲wrap_content */
/* * scaleType屬性 * android:scaleType屬性,設置圖片的填充方式,有以下常量值: * matrix 用矩陣來繪製,動態縮小放大圖片來顯示 * fitXY 把圖片不按比例擴大/縮小到View的大小限時 * fitStart 把圖片按比例擴大/縮小到View的寬度,顯示在View的上部分位置 * fitCenter 把圖片按比例擴大/縮小到View的寬度,居中顯示 * fitEnd 把圖片按比例擴大/縮小到View的寬度,顯示在View的下部分位置 * center 按圖片的原來size居中顯示,當圖片長/寬超過View的長/寬,則截取圖片的居中部分顯示 * centerCrop 按比例擴大圖片的size居中顯示,使得圖片長(寬)等於或大於View的長(寬) * centerInside 將圖片的內容完整居中顯示,經過按比例縮小或原來的size使得圖片長/寬等於或小於View的長/寬 * android:hint 將圖片渲染成指定的顏色 */
public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener { private CheckBox checkBox1; private CheckBox checkBox2; private CheckBox checkBox3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /** * CheckBox(複選框) * 複選框容許用戶從一組選擇中選擇一個或多個,設置選中和未選中可用setChecked(),參數爲true或false */ checkBox1 = (CheckBox) findViewById(R.id.checkbox_id1); checkBox2 = (CheckBox) findViewById(R.id.checkbox_id2); checkBox3 = (CheckBox) findViewById(R.id.checkbox_id3); checkBox1.setOnCheckedChangeListener(this); checkBox2.setOnCheckedChangeListener(this); checkBox3.setOnCheckedChangeListener(this); } @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { switch (compoundButton.getId()) { case R.id.checkbox_id1: if (b) { Toast.makeText(this, "你選中了小說", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "你取消了小說", Toast.LENGTH_SHORT).show(); } break; case R.id.checkbox_id2: if (b) { Toast.makeText(this, "你選中了電影", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "你取消了電影", Toast.LENGTH_SHORT).show(); } break; case R.id.checkbox_id3: if (b) { Toast.makeText(this, "你選中了遊戲", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "你取消了遊戲", Toast.LENGTH_SHORT).show(); } break; } } }
/** * RadioButton(單選按鈕) * 容許用戶從一組選擇中選擇一個選項,必須用RadioGroup將他們分組在一塊兒,才能確保只能選擇一項 * 若國祥讓RadioButton橫着排,則在RadioGroup裏設置android:orientation="horizontal" * 事件處理:onRadioButtonClicked */ radioButton1 = (RadioButton) findViewById(R.id.radioButton1); radioButton2 = (RadioButton) findViewById(R.id.radioButton2); radioButton1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { if (b){ Toast.makeText(MainActivity.this,"你選擇了男",Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this,"你取消選擇了男",Toast.LENGTH_SHORT).show(); } } });
實現RadioButton由兩部分組成,也就是RadioButton和RadioGroup配合使用,RadioGroup是單選組合框,能夠容納多個RadioButton的容器,在沒有RadioGroup的狀況下,RadioButton能夠全都選中,當多個RadioButton被RadioGroup包含的狀況下,RadioButton只能選擇一個,less
並用setOnCheckedChangeListener來對單選按鈕進行監聽編輯器
RadioGroup相關屬性:ide
RadioGroup.getCheckedRadioButtonId();//獲取選中按鈕的id
RadioGroup.clearCheck();
//清除選中狀態
RadioGroup.check(int id);函數
經過參入選項id來設置該選項爲選中狀態,若是傳遞-1做爲指定的選擇標識符來清除單選按鈕組的勾選狀態,至關於調用clearCheck()操做
setOnCheckedChangeListener(RadioGroup.OnCkeckedChangeListener listener);//一個當該單選按鈕組中的單選按鈕勾選狀態發生改變時所要調用的回調函數
addView (View child,int index,ViewGroup.layoutParams params);
使用指定的佈局參數添加一個子視圖,參數 child 所要添加的子視圖,index 將要添加子視圖的位置,params 所要添加的子視圖的佈局參數
RadioButton.getText();
//獲取單選框的值
能夠經過設置以下屬性可使單選按鈕在顯示文本的右邊
android:button="@null" //去除RadioButton 前面的圓點
android:drawableRight="@android:drawable/btn_radio"
調用用adioButton.setButtonDrawable(android.R.color.transparent);
在JAVA代碼中:
去除RadioButton前面的圓點adioButton.setButtonDrawable(android.R.color.transparent);
給ListView設置分割線,只需設置以下兩個屬性:
android:divider="#000" //設置分割線顯示顏色
android:dividerHeight="1px" //此處非0,不然無效
Android (shape,gradient)使用總結
設置背景色能夠經過在res/drawable裏定義一個xml,以下:
<shape> <!-- 實心 --> <solid android:color="#ff9d77"/> <!-- 漸變 --> <gradient android:startColor="#ff8c00" android:endColor="#FFFFFF" android:angle="270" /> <!-- 描邊 --> <stroke android:width="2dp" android:color="#dcdcdc" /> <!-- 圓角 --> <corners android:radius="2dp" /> <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> </shape>
shape是用來定義形狀的,
solid:實心,android:color指填充顏色
gradient定義該形狀裏面爲漸變色填充,startColor其實顏色,endColor結束顏色,angle是漸變角度,必須爲45的整數倍,當angle=0時,漸變色是從左到右,angle=90時,從下往上
stroke:描邊
android:width描邊的寬度,android:color描邊的顏色
咱們還能夠把描邊弄成虛線形式:android:dashWidth橫線的寬度,android:dashGap表示間隔開的距離
corners:圓角
android:radius爲教的弧度,值越大角越圓
<corners android:topRightRadius="20dp" 右上角 android:bottomLeftRadius="20dp" 右下角 android:topLeftRadius="1dp" 左上角 android:bottomRightRadius="0dp" 左下角 />
/** *ToggleButton(Switch用法與ToggleButton同樣) * 一個切換按鈕容許用戶改變二者之間的設置 *經過android:textOff和android:textOn自定義顯示的文字 */ toggleButton = (ToggleButton) findViewById(R.id.toggleButton); aSwitch = (Switch) findViewById(R.id.switch1); aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { if (b) { Toast.makeText(MainActivity.this, "打開", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "關", Toast.LENGTH_SHORT).show(); } } });
<RatingBar android:layout_width="wrap_content" android:layout_height="wrap_content" android:numStars="6" android:stepSize="0.5" android:rating="3.5" android:id="@+id/rating"/>
/** *RatingBar * RatingBar是基於SeekBar和ProgressBar的擴展,用星型來顯示等級評定。使用RatingBar的默認大小是,用戶能夠觸摸、 * 拖動或使用鍵來設置評分 * 有兩種樣式(小峯哥用rationgBar,大風格用rationgBarStylendicator),其中大的只適合知識,不適合與用戶交互 * 當使用能夠與用戶交互的rationgbar時,只能把佈局的寬設置爲wra_content,設置的星星數量(經過函數 * setNumStars(int)或者在XML的佈局文件重定義)將顯示出來(若是設置爲另外一種佈局款的話,後果沒法預知) * 事件處理接口:RatingBar.OnRationgBarChangeLitener * 一個回調函數,當星級進度改變時修改客戶端的星級。 * android:numStars="6"設置星星的總數 * android:stepSize每次增加或減小的步長 * android:rating默認選中的星數 * 若設置android:isIndicator爲true,則不能與用戶交互,只能顯示 */ ratingBar = (RatingBar) findViewById(R.id.rating); ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() { @Override public void onRatingChanged(RatingBar ratingBar, float v, boolean b) { //b:是用戶選的仍是默認的,用戶true //v:是當前選中的星星數量 if (b){ Toast.makeText(MainActivity.this,"rationg="+v,Toast.LENGTH_SHORT).show(); } } });