clockpicker,compoundbutton,radiobutton은
radiogroup으로 감싸야 한다!
안 그럼 checkbox처럼
여러 개가 선택이 된다.
그리들에
viewBinding {
enabled = true
}
바인딩 넣어놓고
activity_main
테마변경
<style name="Theme.CompoundButton12" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<LinearLayout
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:padding="20dp"
android:background="@drawable/layout_rounded"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/check_politics"
android:text="정치"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<CheckBox
android:id="@+id/check_economics"
android:text="경제"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<CheckBox
android:id="@+id/check_entertainments"
android:text="연예"
android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<CheckBox
android:id="@+id/check_sports"
android:text="스포츠"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/radio_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="남성" />
<RadioButton
android:id="@+id/radio_female"
android:text="여성"
android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/radio_trans"
android:text="트랜스젠더"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
</LinearLayout>
<ToggleButton
android:id="@+id/toggleButton"
android:text="토글버튼(표시안됨 의미가 없음)"
android:textOn="ON"
android:textOff="OFF"
android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Switch
android:id="@+id/switch_button"
android:text="블루투스"
android:textOn="온(표시안됨)"
android:textOff="오프(표시안됨)"
android:checked="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/button"
android:text="확인"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textview"
android:background="#8C69CA"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:lineSpacingExtra="5dp"
android:textColor="@color/white"
android:textStyle="bold"
/>
</LinearLayout>
values > strings.xml 에 리소스 넣어놓기
<resources>
<string name="app_name">CompoundButton12</string>
<string-array name="items" >
<item>자바</item>
<item>오라클</item>
<item>HTML5</item>
<item>CSS3</item>
<item>자바스크립트(ES5)</item>
<item>JSP/SERVLET</item>
<item>STANDARD SPRING</item>
<item>Mybatis</item>
<item>SPRING REST API/SWAGGER</item>
<item>JQUERY/AJAX</item>
<item>부트스트랩4</item>
<item>안드로이드</item>
</string-array>
</resources>
res > darwable 에 layout 파일 넣어놓기(태두리모양 설)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF"/>
<stroke android:width="5dp" android:color="#51B5AB" />
<corners android:radius="10dp"/>
</shape>
MainActivity.java
public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
private ActivityMainBinding binding;
//스피너 아이템 저장용
private String[] items;
//[결과 출력을 위한 필드들]
//체크박스
private List<String> interestingLists = new Vector<>();
//라디오버튼
private String gender="남성";
//스피너
private String curiculumn="JSP/SERVLET";
//토글버튼
private String onOff="OFF";
//스위치
private String bluetooth="ON";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
//체크박스 제어
binding.checkEntertainments.setChecked(false);
binding.checkEconomics.setChecked(true);
//최초 선택한값컬렉션에 저장
interestingLists.add("경제");
//라디오버튼제어
//라디오 그룹으로 라디오버튼 체크 해제:clearCheck()
binding.radioGroup.clearCheck();
//라디오그룹으로 라디오버튼 체크 :check(리소스 아이디
binding.radioGroup.check(R.id.radio_male);
//토글버튼
binding.toggleButton.setChecked(false);
//스위치
binding.switchButton.setChecked(true);
//스피너에 아이템 설정하기
items = getResources().getStringArray(R.array.items);
ArrayAdapter<String> adapter=new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item,items);
binding.spinner.setAdapter(adapter);
//처음에 보여줄 아이템 선택:
//xml속성에는 없음. absspinner클래스가 갖고있는 메소드
binding.spinner.setSelection(5);
//리스너 부착
//체크박스랑 토글,스위치 버튼 두개는setOnCheckedChangeListener 이거 붙임
//체크박스
binding.checkEconomics.setOnCheckedChangeListener(this);
binding.checkEntertainments.setOnCheckedChangeListener(this);
binding.checkPolitics.setOnCheckedChangeListener(this);
binding.checkSports.setOnCheckedChangeListener(this);
//토글
binding.toggleButton.setOnCheckedChangeListener(this);
//스위치
binding.switchButton.setOnCheckedChangeListener(this);
//라디오버튼 - 라디오그룹에 리스너 부착하자 그리고 RadioGroup의 OnCheckedChangeListener
//기타텀파운드 버튼들은 CompoundButton.OnCheckedChangeListener
binding.radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
//checkedid는체크된 라디오 버튼의 리소스 id값
//radiogroup의 getchecked 주석가져왕
@Override
public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
gender= ((RadioButton)findViewById(checkedId)).getText().toString();
Log.i("com.kosmo.compound",gender+"선택");
}
});
//스피너에는 setOnItemClickListener가 아니라 setOnItemSelectedListener를 붙여라.
binding.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
/*
adapterView 여기서는spinner를 의미
view ; 하나의 아이템을표시하는덱스트뷰
position:The position of the view in the adapter
id:The row id of the item that is selected
*/
Log.i("com.kosmo.compound",String.format("position:%s,id:%s",position,id));
Log.i("com.kosmo.compound",String.format("커리큘럼:%s",items[position])); //비추
Log.i("com.kosmo.compound",String.format("커리큘럼:%s", ((TextView)view).getText()));
curiculumn=((TextView)view).getText().toString();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {} //얘는 아무 일을하지 않음
});
//확인버튼 이벤트
binding.button.setOnClickListener(view -> {
binding.textview.setText(
String.format("체크박스:%s%n라디오박스:%s%n토글버튼:%s%n스위치:%s%n스피너:%s%n",
interestingLists,
gender,
onOff,
bluetooth,
curiculumn
));
});
}//////////onCreate
//CompoundButton걔열(체크박스/토글/스위치) 버튼 의 checked속성이 변경될 때마다 호출
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean ischecked) {
Log.i("com.kosmo.compound","체크여부:"+ischecked);
if(compoundButton instanceof CheckBox){
if(ischecked){
Log.i("com.kosmo.compound",compoundButton.getText()+"선택");
interestingLists.add(compoundButton.getText().toString());
}
else {
Log.i("com.kosmo.compound",compoundButton.getText()+"해제");
interestingLists.add(compoundButton.getText().toString());
}
}
else if(compoundButton.getId() == R.id.toggleButton){
Log.i("com.kosmo.compound",ischecked? "토글 on" : "토글OFF");
onOff=ischecked? "ON" : "OFF";
}
else {
Log.i("com.kosmo.compound",ischecked? "블루투스 on" : "블루투스OFF");
bluetooth=ischecked? "ON" : "OFF";
}
}
}///////MainActivity
어?
checkEntertainments 아이디 설정한거랑 다름?
바인딩해서 클래스 만들 때 그렇게 바꾼대
'학원 > ANDROID' 카테고리의 다른 글
01/10 (0) | 2023.01.11 |
---|---|
0109 (1) | 2023.01.11 |
01/06 78-2[Android] clockpicker (1) | 2023.01.07 |
01/06 78-1 [Android] Costomview (1) | 2023.01.07 |
01/05 77-4 [Android] resource (0) | 2023.01.07 |