<!--佈局--> <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto"> <TextView android:id="@+id/textviewtitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:text="平時喜歡作什麼事情?" /> <CheckBox android:id="@+id/checkboxall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@id/textviewtitle" android:layout_alignTop="@id/textviewtitle" android:layout_toRightOf="@id/textviewtitle" android:text="全選" /> <!--內容的CheckBox--> <CheckBox android:id="@+id/checkbox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/textviewtitle" android:layout_marginRight="80dp" android:text="玩遊戲" /> <CheckBox android:id="@+id/checkbox2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/textviewtitle" android:layout_toRightOf="@+id/checkbox1" android:text="學習" /> <CheckBox android:id="@+id/checkbox3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/checkbox1" android:text="敲代碼" /> <CheckBox android:id="@+id/checkbox4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/checkbox2" android:layout_toRightOf="@+id/checkbox1" android:text="跑步" /> <CheckBox android:id="@+id/checkbox5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/checkbox3" android:text="游泳" /> <CheckBox android:id="@+id/checkbox6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/checkbox4" android:layout_toRightOf="@+id/checkbox1" android:text="睡覺" /> <TextView android:id="@+id/textviewinfo" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/checkbox5" android:layout_marginTop="20dp" android:text="已選擇:"/> </RelativeLayout> <!--java代碼--> package com.dc.work3; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.TextView; import android.widget.Toast; import java.util.LinkedList; import java.util.List; /** * Created by 怪蜀黍 on 2016/11/4. */ public class MainActivity2s extends AppCompatActivity { private CheckBox checkboxall; private CheckBox checkBox1; private CheckBox checkBox2; private CheckBox checkBox3; private CheckBox checkBox4; private CheckBox checkBox5; private CheckBox checkBox6; private TextView textviewinfo; private List<String> checkedStr; //操做取消一個時,全選取消,這個變量是是不是用戶點擊 private boolean checkFoUser=true; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_2); checkboxall = (CheckBox) findViewById(R.id.checkboxall); checkBox1 = (CheckBox) findViewById(R.id.checkbox1); checkBox2 = (CheckBox) findViewById(R.id.checkbox2); checkBox3 = (CheckBox) findViewById(R.id.checkbox3); checkBox4 = (CheckBox) findViewById(R.id.checkbox4); checkBox5 = (CheckBox) findViewById(R.id.checkbox5); checkBox6 = (CheckBox) findViewById(R.id.checkbox6); textviewinfo = (TextView) findViewById(R.id.textviewinfo); checkBox1.setOnCheckedChangeListener(changeListener); checkBox2.setOnCheckedChangeListener(changeListener); checkBox3.setOnCheckedChangeListener(changeListener); checkBox4.setOnCheckedChangeListener(changeListener); checkBox5.setOnCheckedChangeListener(changeListener); checkBox6.setOnCheckedChangeListener(changeListener); checkboxall.setOnCheckedChangeListener(changeListener); checkedStr=new LinkedList<>(); } public CompoundButton.OnCheckedChangeListener changeListener = new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { switch (buttonView.getId()){ case R.id.checkbox1: case R.id.checkbox2: case R.id.checkbox3: case R.id.checkbox4: case R.id.checkbox5: case R.id.checkbox6: String str=buttonView.getText().toString(); if(isChecked){ checkedStr.add(str); }else { checkedStr.remove(str); } checkboxall.setOnCheckedChangeListener(null); if(checkBox1.isChecked()&&checkBox2.isChecked()&&checkBox3.isChecked()&&checkBox4.isChecked()&&checkBox5.isChecked()&&checkBox6.isChecked()){ //表示若是都選中時,把全選按鈕也選中 checkboxall.setChecked(true); }else { //不然就全選按鈕去不選中,可是這樣會觸發checkboxall的監聽,會把全部的都取消掉 checkboxall.setChecked(false); } checkboxall.setOnCheckedChangeListener(changeListener); break; case R.id.checkboxall: if(checkFoUser) { checkBox1.setChecked(isChecked); checkBox2.setChecked(isChecked); checkBox3.setChecked(isChecked); checkBox4.setChecked(isChecked); checkBox5.setChecked(isChecked); checkBox6.setChecked(isChecked); break; } } StringBuffer sb=new StringBuffer(); for(String str:checkedStr){ sb.append(str+","); } if(sb.length()>0){ //設置長度爲長度-1,去除最後的「,」 sb.setLength(sb.length()-1); } textviewinfo.setText("已選擇:"+sb.toString()); } }; }
<!--佈局--> <?xml version="1.0" encoding="utf-8"?> <!--雖然高度是充滿,可是他是包裹的--> <!--android:scrollbars="none"無滾動條--> <!--android:overScrollMode="never" 滾動條到頭的提示模式--> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="none" android:overScrollMode="never"> <!--注意,ScrolView只能有一個子控件,要想有多個空間,使用Linear Layout包裹--> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!--橫向滾動條--> <!--由於是橫向滾動條,因此無論你寬度設置多少,他始終是充滿的--> <HorizontalScrollView android:layout_width="match_parent" android:layout_height="wrap_content"></HorizontalScrollView> <!--進度條--> <!--android:indeterminate="true" 設置進度條不肯定值的進度條--> <!--android:progressDrawable="@drawable/progress_bar_drawable"咱們自定義的進度條--> <!--高度 android:maxHeight="2dp" android:minHeight="2dp"--> <ProgressBar android:id="@+id/progress_bar" style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="10" android:progress="4" android:secondaryProgress="6" android:progressDrawable="@drawable/progress_bar_drawable" /> <!--能夠拖拽的進度條--> <!-- android:thumb="@mipmap/ic_launcher"自定義圖片--> <!-- android:thumbOffset="100dp" 偏移量,通常不會用到--> <!--android:thumbTint="#0ff" 着色,無論什麼圖片仍是默認,顏色都改變--> <!--鼠標懸浮上邊這句話時,他的意思是,21版本纔可使用,當前版本是16--> <SeekBar android:id="@+id/seek_bar" android:layout_width="match_parent" android:layout_height="wrap_content" android:thumb="@mipmap/ic_launcher" android:thumbTint="#0ff" android:progress="50"/> <!-- android:visibility="invisible" 默認隱藏--> <!--調整文本框的大小盡可能使用邊來改變--> <TextView android:id="@+id/progress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="50dp" android:paddingRight="50dp" android:paddingTop="20dp" android:paddingBottom="20dp" android:background="#cdcdcd" android:text="0%" android:textSize="22sp" android:visibility="invisible" android:layout_gravity="center_horizontal"/> <!--注意使用時,寬度和高度都使用包裹--> <!--android:stepSize="1"每次增加1--> <!--android:isIndicator="true" 是否做爲指示器,好比QQ等級--> <!--style="?android:attr/ratingBarStyleSmall"--> <!--hdpi是mdpi的1.5倍,xmdpi是m的2倍--> <RatingBar android:layout_width="wrap_content" android:layout_height="wrap_content" android:numStars="5" android:rating="1.5" android:stepSize="0.5" android:progressDrawable="@drawable/rating_bar_drawable" android:minHeight="21dp" android:maxHeight="21dp"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/bt1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="減小"/> <Button android:id="@+id/bt2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="增長"/> </LinearLayout> <!--爲了看清楚效果,這裏咱們把高度設爲20dp,text設置多點字符,這樣就超出了屏幕--> <TextView android:layout_width="20dp" android:layout_height="wrap_content" android:text="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </ScrollView> 具體功能實現的java代碼 package com.dc.work3; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.ImageView; import android.widget.RadioGroup; import android.widget.Spinner; /** * Created by 怪蜀黍 on 2016/11/4. */ public class MainActivity_radiobutton extends AppCompatActivity { private RadioGroup rg; private Spinner spinner; //重寫一個參數的處事方法 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_radiobutton); rg= (RadioGroup) findViewById(R.id.rg1); rg.setOnCheckedChangeListener(changeLis); //若是沒有任何選中項,返回-1 // rg.getCheckedRadioButtonId(); //選中指定項 // rg.check(R.id.rb1); //清除選中項 // rg.clearCheck(); ImageView img=(ImageView) findViewById(R.id.iv); // 設置圖片 img.setImageResource(R.mipmap.ic_launcher); //設置縮放類型 img.setScaleType(ImageView.ScaleType.CENTER); spinner=(Spinner)findViewById(R.id.spinner); spinner.setOnItemSelectedListener(itemSelectedListener); } private AdapterView.OnItemSelectedListener itemSelectedListener =new AdapterView.OnItemSelectedListener() { @Override//當項選中時 參數1:觸發事件的控件 2:選中的視圖 3:選中視圖的id public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { } @Override//當沒有項選中時 public void onNothingSelected(AdapterView<?> parent) { } }; private RadioGroup.OnCheckedChangeListener changeLis=new RadioGroup.OnCheckedChangeListener() { @Override //參數2:選中者的id public void onCheckedChanged(RadioGroup group, int checkedId) { //獲取選中項的數據 String str=spinner.getSelectedItem().toString(); //獲取選中項的下標 int position=spinner.getSelectedItemPosition(); //獲取選中項的id long id= spinner.getSelectedItemId(); Log.e("aaaa",str+"========="+position+"========="+id); } }; }