Android| |Android常见的控件

除了之前介绍的Button之类的,将还有其他的控件

1、RadioGroup和RadioButton的使用方法

2、CheckBox的使用方法

3、Toast的基本用法

我们通过一个例子来了解这些控件

MainActivity.java

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    //对控件对象进行声明
    private RadioGroup genderGroup = null;
    private RadioButton femaleButton = null;
    private RadioButton maleButton = null;
    private CheckBox swimBox = null;
    private CheckBox runBox = null;
    private CheckBox readBox = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //通过控件的ID来得到代表控件的对象
        genderGroup = (RadioGroup)findViewById(R.id.genderGroup);
        femaleButton = (RadioButton)findViewById(R.id.femaleButton);
        maleButton = (RadioButton)findViewById(R.id.maleButton);
        swimBox = (CheckBox)findViewById(R.id.swim);
        runBox = (CheckBox)findViewById(R.id.run);
        readBox = (CheckBox)findViewById(R.id.read);
        //为RadioGroup设置监听器,需要注意的是,这里的监听器和Button控件的监听器有所不同
        genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // TODO Auto-generated method stub
                if(femaleButton.getId() == checkedId){
                    System.out.println("famale");
                    Toast.makeText(MainActivity.this, "famle", Toast.LENGTH_SHORT).show();
                }
                else if(maleButton.getId() == checkedId)
                {
                    System.out.println("male");
                }
            }
        });

        //为多选按钮添加监听器
        swimBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                if(isChecked)
                {
                    System.out.println("swim is checked");
                }
                else
                {
                    System.out.println("swim is unchecked");
                }
            }
        });
        runBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                if(isChecked)
                {
                    System.out.println("run is checked");
                }
                else
                {
                    System.out.println("run is unchecked");
                }
            }
        });
        readBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                if(isChecked)
                {
                    System.out.println("read is checked");
                }
                else
                {
                    System.out.println("read is unchecked");
                }
            }
        });
    }

}

 

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello"
        />
    <RadioGroup
        android:id="@+id/genderGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
        <RadioButton
            android:id="@+id/femaleButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/female"
            />
        <RadioButton
            android:id="@+id/maleButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/male"
            />
    </RadioGroup>
    <CheckBox
        android:id="@+id/swim"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/swim"
        />
    <CheckBox
        android:id="@+id/run"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/run"
        />
    <CheckBox
        android:id="@+id/read"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/read"
        />
</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="chao.pers.activity_07">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/hello"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!-- ATTENTION: This was auto-generated to add Google Play services to your project for
             App Indexing.  See https://g.co/AppIndexing/AndroidStudio for more information. -->
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
    </application>

</manifest>

另外还有一个string.xml文件

<resources>
    <string name="hello">activity_07</string>
    <string name="male">男</string>
    <string name="female">女</string>
    <string name="swim">swim</string>
    <string name="run">run</string>
    <string name="read">read</string>
</resources>

结果如图下:

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 控件集合是指在Android开发中,用于构建用户界面的一组预定义控件的集合。Android提供了丰富的控件可供开发者使用,包括文本框、按钮、图像视图、列表视图、下拉框等。这些控件可以通过XML布局文件或者Java代码动态创建和配置。 Android 控件集合具有以下特点: 1. 多样性:Android控件集合中包含了丰富的控件种类,满足了不同应用场景下的需求。开发者可以根据具体的界面设计要求选择合适的控件进行使用。 2. 可定制性:Android控件集合提供了丰富的配置属性,开发者可以通过设置这些属性来满足自己的设计需求。同时还可以通过自定义控件继承或覆盖Android原生控件,实现更加个性化的效果。 3. 事件监听:Android控件集合支持事件监听机制,开发者可以为控件添加相应的事件处理器,对用户的交互行为做出响应。比如,可以为按钮控件添加点击事件监听器,实现点击按钮后执行相应的操作。 4. 布局管理:Android控件集合中的控件可以通过各种布局管理器进行灵活的排列和组合。比如,线性布局、相对布局、帧布局等。开发者可以根据需求选择合适的布局管理器,实现想要的页面布局效果。 综上所述,Android控件集合提供了丰富多样的控件供开发者使用,具备较高的可定制性和灵活性,方便开发者构建出各种各样的用户界面。通过合理运用和配置控件集合,可以实现强大的Android应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值