android笔记6-常用控件的介绍二

来源:互联网 发布:淘宝买家信用有什么用 编辑:程序博客网 时间:2024/05/16 14:11

单选按钮的使用

一个常用的公共方法

Public void toggle()

这里用了button处理事件的另一种方法:在xml布局文件中通过android:onclick = "selfDestruct" 来完成。

<?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:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="单选按钮的使用" /><RadioGroup     android:id="@+id/rg"    android:layout_width="wrap_content"android:layout_height="wrap_content"        >        <RadioButton    android:id="@+id/radioButton1"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="男" />    <RadioButton    android:id="@+id/radioButton2"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="女" /></RadioGroup><Button    android:id="@+id/button1"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:onClick="selfDestrut"    android:text="选择性别" /></LinearLayout>

public class Android_radio1Activity extends Activity{/** Called when the activity is first created. */private Button button;private RadioGroup group;@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);// button = (Button)this.findViewById(R.id.button1);group = (RadioGroup)this.findViewById(R.id.rg);}public void selfDestrut(View view)    {    int len = group.getChildCount();    String msg = "";    for(int i = 0 ; i < len ; i++)    {    RadioButton rb = (RadioButton)group.getChildAt(i);    if(rb.isChecked())    {    msg = rb.getText().toString();    break;    }    }    Toast.makeText(Android_radio1Activity.this, msg, 1).show();//设置为显示1秒    }}

开关按钮的使用

Java代码中的实现

用开关按钮实现两种排列方式的转换

java代码中对垂直/水平布局的设置


<?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" >    <ToggleButton        android:id="@+id/tog"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:checked="true"        android:textOff="横向排列"        android:textOn="纵向排列" />    <LinearLayout        xmlns:android="http://schemas.android.com/apk/res/android"        android:id="@+id/mylayout"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="vertical" >        <Button            android:id="@+id/button1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Button1" />        <Button            android:id="@+id/button2"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Button2" />        <Button            android:id="@+id/button3"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Button3" />    </LinearLayout></LinearLayout>


public class Main extends Activity {    /** Called when the activity is first created. */private ToggleButton tb;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        tb = (ToggleButton)this.findViewById(R.id.tog);        final LinearLayout linearLayout = (LinearLayout)this.findViewById(R.id.mylayout);        tb.setOnCheckedChangeListener(new OnCheckedChangeListener(){@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked){// TODO Auto-generated method stubif(isChecked){linearLayout.setOrientation(1);//表示设置垂直布局}else linearLayout.setOrientation(0);}});    }}


0 0