【Android】Android常用控件的基本使用

TextView


TextView用于显示一段文本信息,如下所示:

TextView

布局代码如下:

<TextView
    android:id="@+id/text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#000000"
    android:gravity="center"
    android:text="TextVew"
    android:textColor="#00ff00"
    android:textSize="24sp" />

Button


Button是程序中用于和用户交互的重要控件,可以通过点击Button来触发事件:

Button

布局代码如下:

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

Button点击事件的监听器,见Button中的OnClickListener实现方法

EditText


EditText也是程序中和用户交互的一个重要控件,它允许用户在TextView中编辑内容,然后在程序中对这些内容进行处理:

EditText

布局代码如下:

<EditText
    android:textColorHint="#00ffff"
    android:hint="Type something here"
    android:maxLines="3"
    android:id="@+id/edit_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

ImageView


ImageView用于在界面上显示图片的一个控件:

ImageView

布局代码如下:

<ImageView
    android:src="@drawable/img_1"
    android:id="@+id/image_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

ProgressBar


ProgressBar用于在界面上显示一个进度条,表示正在加载数据:

ProgressBar

布局代码如下:

<ProgressBar
    android:id="@+id/progress_bar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

AlertDialog


AlertDialog可以在当前界面上弹出一个对话框,置顶与其他界面元素之上,能够屏蔽其他控件的交互:

AlertDialog

代码如下:

AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
alert.setTitle("Alert");
alert.setMessage("Something important");
alert.setCancelable(false);
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(MainActivity.this, "You clicked OK", Toast.LENGTH_SHORT).show();
    }
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(MainActivity.this, "You clicked cancel", Toast.LENGTH_SHORT).show();
    }
});
alert.show();

ProgressDialog


ProgressDialog和AlertDialog类似,能够屏蔽其他控件的交互能力,ProgressDialog还会在对话框中显示一个进度条:

ProgressDialog

代码如下:

ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("ProgressDialog");
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(false);
progressDialog.show();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值