, , , XML .
drawableRight ( Drawable Right) :
<RadioButton android:id="@+id/radio0"... android:drawableRight="@drawable/ic_launcher" android:text="RadioButton" />setCompoundDrawables(left, top, right, bottom):
radiobutton.setCompoundDrawables(null, null, getResources().getDrawable(R.drawable.rightpic), null);, null. .
-, . .
<?xml version="1.0" encoding="utf-8"?><resources> <string name="app_name"> (RadioButton)</string> <string name="current_pick"> </string> <string name="color_red"></string> <string name="color_green"></string> <string name="color_blue"></string> <string name="color_gray"></string></resources>LinearLayout. " ". , , .
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/current_pick" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:background="#666666" android:gravity="center" android:text="@string/current_pick" android:textColor="#ffffff" /> <LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/textViewRed" android:layout_width="fill_parent" android:layout_height="20dp" android:layout_gravity="center" android:layout_weight="1" android:background="#ff0000" android:text="@string/color_red" android:textColor="#660000" /> <TextView android:id="@+id/textViewGreen" android:layout_width="fill_parent" android:layout_height="20dp" android:layout_gravity="center" android:layout_weight="1" android:background="#00ff00" android:text="@string/color_green" android:textColor="#006600" /> <TextView android:id="@+id/textViewBlue" android:layout_width="fill_parent" android:layout_height="20dp" android:layout_gravity="center" android:layout_weight="1" android:background="#0000ff" android:text="@string/color_blue" android:textColor="#000066" /> <TextView android:id="@+id/textViewGray" android:layout_width="fill_parent" android:layout_height="20dp" android:layout_gravity="center" android:layout_weight="1" android:text="@string/color_gray" /> </LinearLayout> <RadioGroup android:id="@+id/radioGroup1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#333333" android:orientation="horizontal" > <RadioButton android:id="@+id/radio_red" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="24dp" android:layout_weight="1" /> <RadioButton android:id="@+id/radio_blue" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="24dp" android:layout_weight="1" /> <RadioButton android:id="@+id/radio_green" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="24dp" android:layout_weight="1" /> <RadioButton android:id="@+id/radio_gray" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="24dp" android:layout_weight="1" android:checked="true" /> </RadioGroup> </LinearLayout>, RadioButton RadioGroup. , . , . (android:checked="true").
|
|
. -.
OnClickListener radioListener;// TextViewTextView tvPick;:
radioListener = new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub RadioButton rb = (RadioButton)v; switch (rb.getId()) { case R.id.radio_red: tvPick.setBackgroundColor(Color.parseColor("#ff0000")); break; case R.id.radio_green: tvPick.setBackgroundColor(Color.parseColor("#0000ff")); break; case R.id.radio_blue: tvPick.setBackgroundColor(Color.parseColor("#00ff00")); break; case R.id.radio_gray: tvPick.setBackgroundColor(Color.parseColor("#666666")); break; default: break; } }}; tvPick = (TextView)findViewById(R.id.current_pick); RadioButton rb_red = (RadioButton)findViewById(R.id.radio_red);rb_red.setOnClickListener(radioListener); RadioButton rb_green = (RadioButton)findViewById(R.id.radio_green);rb_green.setOnClickListener(radioListener); RadioButton rb_blue = (RadioButton)findViewById(R.id.radio_blue);rb_blue.setOnClickListener(radioListener); RadioButton rb_gray = (RadioButton)findViewById(R.id.radio_gray);rb_gray.setOnClickListener(radioListener);, . , .
|
|
, . setOnCheckedChangeListener(). RadioGroup RadioButton . - .
@Overridepublic void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); RadioGroup radiogroup = (RadioGroup) findViewById(R.id.radioGroup1); radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // TODO Auto-generated method stub switch (checkedId) { case -1: Toast.makeText(getApplicationContext(), "No choice", Toast.LENGTH_SHORT).show(); break; case R.id.radio0: Toast.makeText(getApplicationContext(), "Radio0", Toast.LENGTH_SHORT).show(); break; case R.id.radio1: Toast.makeText(getApplicationContext(), "Radio1", Toast.LENGTH_SHORT).show(); break; case R.id.radio2: Toast.makeText(getApplicationContext(), "Radio2", Toast.LENGTH_SHORT).show(); break; default: break; } } });}. . .
, onCheckedChanged() RadioGroup, OnCheckedChangeListener() . -1 , clearCheck().
clearCheck()
, . - . - clearCheck(), RadioGroup.
RadioGroup radiogroup; // public void onClick(View view) { // radiogroup.clearCheck();}getCheckedRadioButtonId()
RadioGroup getCheckedRadioButtonId(), .
public void onClick(View view) { int checkedRadioButtonId = radiogroup.getCheckedRadioButtonId(); // id RadioButton myradiobutton = (RadioButton) findViewById(checkedRadioButtonId); Toast.makeText(getApplicationContext(), myradiobutton.getText(), Toast.LENGTH_SHORT).show();}indexOfChild():
OnCheckedChangeListener radioGroupOnCheckedChangeListener = new OnCheckedChangeListener(){ @Override public void onCheckedChanged(RadioGroup group, int checkedId) { RadioButton checkedRadioButton = (RadioButton)radioGroup.findViewById(checkedId); int checkedIndex = radioGroup.indexOfChild(checkedRadioButton); textCheckedIndex.setText("checkedIndex = " + checkedIndex); }};?
, , .
:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linearLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:padding="10dip" > <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="" /> <RadioGroup android:id="@+id/radioGroup1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="24dp" > <RadioButton android:id="@+id/radioHusband" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="" /> <RadioButton android:id="@+id/radioKitten" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" /> </RadioGroup> </LinearLayout>:
|
|
-. Android android.widget.Button.. ImageButton (android.widget.ImageButton), .
ToggleButton onCheckedChanged().
ToggleButton. , -:
Button Form Widgets:
(android:layout_width="match_parent"), android:layout_margin ( layout_marginRight layout_marginLeft) (- ).
TextView, : textColor, textSize .
, RadioButton.
c (Button) (SmallButton) Widgets, . Design Palette. Layouts, View Text Fields.
(Button) (SmallButton). :
1) - ,
2) ( ),
3) ,
|
|
4) SmallButton .
.
1. Button Palette . . Button, , :
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="223dp"
android:layout_marginEnd="223dp" />
. :
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
2. background. background. Resourses Project, System, Color . . - RGB ( , ). , 255 Red, Green 0 Blue. , # - 16- FFFF00, 0 256 16- (.2.3).
.2.3.
.2.4.
OK: 1) , , (.2.4), 2) ( ) :
android:background="#ffffff00">
3. , , Text. . (android:text=""). Color ARGB 255 lpha- Red. :
android:textColor="#ffff0000" />
5. SmallButton - ( ARGB, : 255-0-0-0), - () ( ARGB, : 255-255-255-255) C.
.2.5.
:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/button"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#ffffff00"
android:textColor="#ffff0000" />
.2.5.
:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/button"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#ffffff00"
android:textColor="#ffff0000" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/button2"
|
|
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="#ff000000"
android:textColor="#ffffffff" />
, XML- Palette Properties. activity_main.xml , . . .