19 UI_常用组件-BasicViews-button-checkbox-radio

UI_常用组件-BasicViews-button-checkbox-radio

 

 


 

1、关于Button的演示案例:

main.xml: 

<?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" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

    <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOn="男"
        android:textOff="女"
        android:onClick="test"/>

</LinearLayout>

 

Test_buttonActivity.java:

package test.button;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.widget.ToggleButton;

public class Test_buttonActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
    public void test(View view){
        ToggleButton toggleButton =(ToggleButton) view;
        Toast.makeText(this, toggleButton.getText(), 0).show();
    }
}

 

运行结果(点击ToggleButton)

   

 

 

 

 


2、关于RadioGroup单选按钮的演示案例:

main.xml: 

<?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" >
    <RadioButton
        android:id="@+id/radioButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton" />

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="语文" 
            android:tag="1"/>

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="数学" 
            android:tag="2"/>

        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="英语" 
            android:tag="3"/>

    </RadioGroup>

</LinearLayout>

 

Test_buttonActivity.java:

package test.button;
import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

public class Test_buttonActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radioGroup1);
        radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton button = (RadioButton)group.findViewById(checkedId);
                Toast.makeText(getApplicationContext(),(String)button.getTag()+ button.getText(), 0).show();
            }
        });
    }    
}

 

运行结果(点击)

     

 

 

 

 


 

3、关于CheckBox复选框的演示案例:

main.xml: 

<?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" >
    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="语文" 
        android:tag="1"/>

    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="数学" 
        android:tag="2"/>

    <CheckBox
        android:id="@+id/checkBox3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="英语" 
        android:tag="3"/>
  
</LinearLayout>

 

Test_buttonActivity.java:

package test.button;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.*;
import android.widget.Toast;
public class Test_buttonActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        CheckBox box1=(CheckBox) findViewById(R.id.checkBox1);
        CheckBox box2=(CheckBox) findViewById(R.id.checkBox2);
        CheckBox box3=(CheckBox) findViewById(R.id.checkBox3);
        box1.setOnCheckedChangeListener(myListener);
        box2.setOnCheckedChangeListener(myListener);
        box3.setOnCheckedChangeListener(myListener);
        box1.setOnCheckedChangeListener(myListener);        
    }
    private OnCheckedChangeListener myListener = new OnCheckedChangeListener(){
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked){
                Toast.makeText(getApplicationContext(), buttonView.getText()+"被选中了", 0).show();
            }else{
                Toast.makeText(getApplicationContext(), buttonView.getText()+"被取消了", 0).show();
            }
            
        }
    };
    
}

 

运行结果(点击)

  

 

posted @ 2017-07-21 10:24  维尼少少  阅读(156)  评论(0编辑  收藏  举报