Switch-RadioButton-CheckBox例子

来源:互联网 发布:mycat 性能优化 编辑:程序博客网 时间:2024/06/08 05:45

<span style="font-size: 18px; ">这里具体这么用我就不解释了,下面我就对他们做一个例子讲解</span>
public class MainActivity extends Activity {private RadioButton radio1,radio2,radio3;private CheckBox check;private Switch switch1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);radio1=(RadioButton) findViewById(R.id.radio1);radio2=(RadioButton) findViewById(R.id.radio2);radio3=(RadioButton) findViewById(R.id.radio3);check=(CheckBox) findViewById(R.id.check);switch1=(Switch) findViewById(R.id.switch1);}public void radioClick(View v){RadioButton button=(RadioButton) v;boolean isChecked=button.isChecked();switch (v.getId()){case R.id.radio1:if(isChecked){Toast.makeText(MainActivity.this, button.getText(), Toast.LENGTH_LONG).show();}break;case R.id.radio2:if(isChecked){Toast.makeText(MainActivity.this, button.getText(), Toast.LENGTH_LONG).show();}break;case R.id.radio3:if(isChecked){Toast.makeText(MainActivity.this, button.getText(), Toast.LENGTH_LONG).show();}break;default:break;}}public void switchClick(View v){boolean isChecked=((Switch)v).isChecked();switch (v.getId()) {case R.id.switch1:if(isChecked){Toast.makeText(MainActivity.this, "打开", Toast.LENGTH_LONG).show();}else{Toast.makeText(MainActivity.this, "关闭", Toast.LENGTH_LONG).show();}break;default:break;}}public void checkClick(View v){CheckBox box=(CheckBox) v;boolean isChecked=box.isChecked();switch (v.getId()) {case R.id.check:if(isChecked){Toast.makeText(MainActivity.this, check.getText(), Toast.LENGTH_LONG).show();}break;default:break;}}}
最后说下布局:

<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:orientation="vertical"    tools:context=".MainActivity" >    <RadioGroup        android:id="@+id/radioGroup1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"         android:orientation="horizontal">        <RadioButton            android:id="@+id/radio1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"              android:onClick="radioClick"            android:checked="true"            android:text="男" />        <RadioButton            android:id="@+id/radio2"            android:layout_width="wrap_content"            android:layout_height="wrap_content"              android:onClick="radioClick"            android:text="女" />        <RadioButton            android:id="@+id/radio3"            android:layout_width="wrap_content"            android:layout_height="wrap_content"             android:onClick="radioClick"            android:text="保密" />    </RadioGroup>    <Switch    android:id="@+id/switch1"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:textOn="打开"    android:textOff="关闭"    android:onClick="switchClick"    android:text="开启功能"/><CheckBox     android:id="@+id/check"    android:layout_width="match_parent"    android:layout_height="wrap_content"     android:onClick="checkClick"    android:text="是否有用"/></LinearLayout>

这里是代码的连接:http://download.csdn.net/detail/kluing/7623545


0 0