您的位置:首页 > 移动开发 > Android开发

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

2017-01-25 15:08 666 查看

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
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
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
android:src="@drawable/img_1"
android:id="@+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />


ProgressBar

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



布局代码如下:

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


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 = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("ProgressDialog");
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(false);
progressDialog.show();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息