Android学习之五:android一些基本控件

每一个GUI开发工具都会提供一些基本的控件,例如Label和Button 等,下面我们来看下Android的一些基本控件。

  1. Label:就是只用来显示些文本信息,而且不需要编辑的控件,在Android中是使用TextView控件的。我们来看一下在xml文件下面怎么定义该控件,我们来看以下的xml代码:

    <TextView android:layout_width=”fill_parent”
        android:layout_height=”wrap_content”
        android:text=”hello world”
    />

    我们在代码里定义了TextView的宽度,高度和显示文本等,当然我们还可以定义它显示的样式和颜色等。我们看运行效果

    2010-10-20-01.jpg?psid=1

  2. Button:点击按钮,我们在Android 学习之四中曾经创建了一个带Button控件的示例程序,我们当时是在代码中设置它的监听事件的,现在我们可以直接在xml文件中设置其点击事件要触发的方法,看下面的xml代码:

    <Button android:layout_width=”fill_parent”
            android:layout_height=”wrap_content”
            android:text=”click me”
           android:onClick=”dosomething”
    />

    接下来我们只需要在java代码中定义名称我dosomething的方法就可以了,代码如下:

    public class NowActivity extends Activity  {
        /** Called when the activity is first created. */
        Button btn;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
        }
        public void dosomething(View btn){
            TextView t=(TextView)this.findViewById(R.id.tv);
            t.setText(“click”);
        }
    }

  3. ImageView:显示 Image,要注意到是android:src写图片地址的时候不需要填写图片的扩展名,例如本例 calendar.png,只需要写calendar就可以了。也可以通过setImageURI()来设置图片内容。

    <ImageView
            android:layout_width=”fill_parent”
            android:layout_height=”wrap_content”
            android:id=”@+id/icon1″
            android:src=”@drawable/calendar”
            android:background=”#ffffffff”
    />

    2010-10-20-02.jpg?psid=1

  4. ImageButton:图片按钮控件。
  5. EditText:文本编辑框。经常用到属性有:
android:autoText:提供自动拼写检查。 android:capitalize:设置英文字母大写类型。 android:digits:设置只能输入的数字。 android:singleline:控制是否单行输入。

<EditText
        android:layout_width=”fill_parent”
        android:layout_height=”wrap_content”
        android:capitalize=”sentences”
        android:text=”Hello”
        android:digits=”1234″
/>

6.CheckBox:常用的你可以使用 isChecked()来判断选中状态,setChecked()来使之为选中状态,toggle()使之选中状态变为当前相反。xml代码如下

<CheckBox android:layout_width=”fill_parent”
          android:layout_height=”wrap_content”
          android:id=”@+id/chktest”
          android:text=”text”
/>

java代码调用如下:

CheckBox chk=(CheckBox)this.findViewById(R.id.chktest);
chk.setOnCheckedChangeListener(new OnCheckedChangeListener(){

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                // TODO Auto-generated method stub
                if(isChecked){
                    chk.setText(“checked”);
                }
                else{
                    chk.setText(“unchecked”);
                }
            }
        });

7.RadioButton:一般和RadioGroup一块使用,看下面的xml代码:

<RadioGroup android:layout_width=”fill_parent”
            android:layout_height=”wrap_content”
            android:id=”@+id/group1″>
    <RadioButton android:layout_width=”fill_parent”
                android:layout_height=”wrap_content”
                android:text=”one”
                android:id=”@+id/rb1″/>
    <RadioButton android:layout_width=”fill_parent”
                android:layout_height=”wrap_content”
                android:text=”two”
                android:id=”@+id/rb2″/>
</RadioGroup>

可以使用RadioGroup的check(id),和 clearCheck()来对RadioButton来操作。

一些比较有用的属性:

  • android:nextFocusDown
  • android:nextFocusLeft
  • android:nextFocusRight
  • android:nextFocusUp

转载于:https://www.cnblogs.com/zjmsky/archive/2010/12/07/1898804.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值