Android提供了大量的UI控件,合理的使用控件可以编写出很漂亮的页面,下面介绍几种很常见的控件。
1.TestView:用于在界面上显示一段文本信息
2.Button:在界面上显示一个按钮,经常使用button.setOnclickListen(this)触发按钮点击事件
3.EditText:在控件里输入和编辑内容
4.ImageView:在界面上展示图片,在res目录下新建一个drawble-xhdpi目录,将图片复制到该目录
5.ProgressBar:在界面上显示一个进度条
6.AlertDialog:在界面上弹出一个对话框,用于提示重要的内容或警告信息
................
Android控件的用法都很相似:给控件定义一个id,再指定控件的宽度和高度,然后再适当加些控件特有的属性。
除了显示所列的这些基本控件外,我把这些控件和按钮点击事件结合在一起实例化学习,如:
1.点击Button01,显示AlertDialog (图2)
2.在EditText输入内容,点击Button02,通过Toast显示(图3)
3.点击Button03,图片变换(图4)
4.每点击一次Button04,水平进度条会更新进度条(图5)
创建一个UITest项目,写好代码后运行,首界面效果和点击各按钮效果如下:





Activity_main.xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#ff00"
android:textSize="24sp"
android:text="This is TextView!"
/>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button01"/>
<Button
android:id="@+id/button02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button02"/>
<Button
android:id="@+id/button03"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button03"/>
<Button
android:id="@+id/button04"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button04"/>
<EditText
android:id="@+id/edt_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Type sonthing here"
android:maxLines="2"
/>
<ImageView
android:id="@+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/timg"/>
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ProgressBar
android:id="@+id/progressBarhorizontal"
style="?android:attr/progressBarStyleHorizontal"
android:max="100"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
MainActivity代码如下:
package com.example.uiwidgettest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText editText;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=(EditText)findViewById(R.id.edt_text);
Button button=(Button) findViewById(R.id.button);
Button button2=(Button) findViewById(R.id.button02);
Button button3=(Button) findViewById(R.id.button03);
Button button4=(Button) findViewById(R.id.button04);
final ProgressBar progressBar=(ProgressBar)findViewById(R.id.progressBarhorizontal) ;
imageView=(ImageView)findViewById(R.id.image_view);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//此处添加逻辑
switch (v.getId()){
case R.id.button:
AlertDialog.Builder dialog=new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("this is Dialog" );
dialog.setMessage("Something is important");
dialog.setCancelable(false);//false 通过back键不能取消提示窗口 true可以取消
dialog.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
dialog.show();
break;
default:
break;
}
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button02:
String inputText=editText.getText().toString();
Toast.makeText(MainActivity.this,inputText,Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button03:
imageView.setImageResource(R.drawable.timg_view);
break;
default:
break;
}
}
});
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button04:
int progress=progressBar.getProgress();
progress=progress+10;
progressBar.setProgress(progress);
break;
default:
break;
}
}
});
}
}
使用AlertDialog控件的activity_alert_dialog.xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AlertDialogActivity">
</android.support.constraint.ConstraintLayout>
使用AlertDialog控件的AlertDialogActivity类代码如下:
package com.example.uiwidgettest;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
public class AlertDialogActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alert_dialog);
}
}
好了,一些常用控件用法就学习好啦~
本文详细介绍了Android中常用的UI控件,包括TextView、Button、EditText、ImageView、ProgressBar和AlertDialog等的使用方法,并通过实例展示了如何结合按钮点击事件进行操作。
7465

被折叠的 条评论
为什么被折叠?



