常用控件的使用方法

1、TextView :用於在界面上顯示一段文字。

    <TextView
        android:id="@+id/text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="30sp"
        android:textColor="#ff0000"        
        android:text="This is a TextView" />

android:id 表示為該控件定義一個唯一的標識符
android:layout_width 表示設定該控件的寬度
android:layout_height 表示設定該控件的高度
android 中所有的控件都具體這兩個屬性,屬性值包含:match_parent、wrap_content、fill_parent。其中match_parent和fill_parent意義一致,但官方更推薦使用match_parent。
match_parent 表示該控件的大小與父佈局大小一樣,也就是由父佈局決定控件大小
wrap_content 表示該控件的大小由控件本身內容決定
android:gravity 表示該控件內容的對齊方式。可選值有:top,bottom,left,right,center。
android:textSize 表示設定控件內容的大小
android:textColor 表示設定控件內容的顏色。

2、Button 用於在界面上顯示一個按鈕

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

每個button必須添加監聽器,否則button將失效。

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new OnClickListener() {           
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(MainActivity.this, "hello", Toast.LENGTH_SHORT).show();
            }
        });
    }

3、EditText 用於在界面上顯示一個可編輯的文本框

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button)findViewById(R.id.button);
        editText = (EditText)findViewById(R.id.edit_text);
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                editData = editText.getText().toString();
                Log.d("hello", "editData = " + editData);
                if(editData != null & !editData.equals("")){
                    Toast.makeText(MainActivity.this, editData, Toast.LENGTH_SHORT).show();
                }else
                    Toast.makeText(MainActivity.this, "hello1", Toast.LENGTH_SHORT).show();
            }
        });
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值