• 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的宽度,高度和显示文本等,当然我们还可以定义它显示的样式和颜色等。我们看运行效果

    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”
      />

    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
  • 相关阅读:
    win7游戏窗口设置
    怎么在 html 中 动态的加载一个 script
    nodejs+express +jade模板引擎 新建项目
    将大数据利用 BCP 导出SqlServer数据到CSV
    产品经理如何赢得开发人员的尊重和支持?-摘自infoq
    Microsoft TFS 如何显示在Windows 的上下文菜单中
    使用PowerDesigner 设计SQL Server 数据库
    sqlserver 删掉日志文件ldf以后 救命语句
    SqlServer修改数据库文件及日志文件存放位置
    快速备份sqlserver2005以上版本数据库的方法-摘自网络
  • 原文地址:https://www.cnblogs.com/zjmsky/p/1898804.html
  • Copyright © 2020-2023  润新知