1.新建项目<UIWidgetTest>
2.在layout目录下,在activity_main.xml中添加各种自己所需的常用控件
3.在Java目录下,在MainActivity.java中,加载布局,指定各控件的活动
4.在AndroidManifest.xml下,注册主活动
代码如下:
layout——>activity_main.xml(2)
<RelativeLayout
xmlns:
android
=
"http://schemas.android.com/apk/res/android"
xmlns: tools = "http://schemas.android.com/tools"
android :layout_width= "match_parent"
android :layout_height= "match_parent"
android :paddingBottom= "@dimen/activity_vertical_margin"
android :paddingLeft= "@dimen/activity_horizontal_margin"
android :paddingRight= "@dimen/activity_horizontal_margin"
android :paddingTop= "@dimen/activity_vertical_margin"
tools :context= "com.administrator.uiwidgettest.MainActivity" >
<TextView
android :id= "@+id/text_view"
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :gravity= "center"
android :textSize= "24sp"
android :textColor= "#0015ff"
android :text= "This is TextView"
android :layout_alignParentTop= "true"
android :layout_alignParentEnd= "true" />
<Button
android :id= "@+id/button"
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :text= "显示文本框内容"
android :layout_below= "@+id/text_view"
android :layout_alignParentStart= "true" />
<Button
android :id= "@+id/button1"
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :text= "更换图片"
android :layout_below= "@+id/button"
android :layout_alignParentStart= "true" />
<Button
android :id= "@+id/button2"
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :text= "圆形进度条"
android :layout_below= "@+id/button1"
android :layout_alignParentStart= "true" />
<Button
android :id= "@+id/button3"
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :text= "条形进度条"
android :layout_below= "@+id/button2"
android :layout_alignParentStart= "true" />
<Button
android :id= "@+id/button4"
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :text= "警告信息"
android :layout_below= "@+id/button3"
android :layout_alignParentEnd= "true" />
<Button
android :id= "@+id/button5"
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :text= "进度对话框"
android :layout_below= "@+id/button4"
android :layout_alignParentEnd= "true" />
<EditText
android :id= "@+id/edit_text"
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :hint= "Type something here"
android :maxLines= "2"
android :layout_below= "@+id/button4"
android :layout_alignParentStart= "true"
android :layout_marginTop= "48dp" />
<ImageView
android :id= "@+id/image_view"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :gravity= "center"
android :src= "@mipmap/ic_launcher"
android :layout_below= "@+id/edit_text"
android :layout_alignParentStart= "true" />
//ImageView是用于在界面上展示图片的一个控件
<ProgressBar
android :id= "@+id/progress_bar"
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :layout_below= "@+id/button4"
android :layout_alignParentTop= "true" />
//默认的是圆形进度条
//ProgressBar用于在界面上显示一个进度条,表示程序正在加载一些数据
<ProgressBar
android :id= "@+id/progress_bar1"
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :layout_alignParentBottom= "true"
style= "?android:attr/progressBarStyleHorizontal"
android :max= "100" />//通过android:max属性给进度条设置一个最大值
//通过style属性来设置进度条类型,现在是条状的
</RelativeLayout>
xmlns: tools = "http://schemas.android.com/tools"
android :layout_width= "match_parent"
android :layout_height= "match_parent"
android :paddingBottom= "@dimen/activity_vertical_margin"
android :paddingLeft= "@dimen/activity_horizontal_margin"
android :paddingRight= "@dimen/activity_horizontal_margin"
android :paddingTop= "@dimen/activity_vertical_margin"
tools :context= "com.administrator.uiwidgettest.MainActivity" >
<TextView
android :id= "@+id/text_view"
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :gravity= "center"
android :textSize= "24sp"
android :textColor= "#0015ff"
android :text= "This is TextView"
android :layout_alignParentTop= "true"
android :layout_alignParentEnd= "true" />
<Button
android :id= "@+id/button"
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :text= "显示文本框内容"
android :layout_below= "@+id/text_view"
android :layout_alignParentStart= "true" />
<Button
android :id= "@+id/button1"
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :text= "更换图片"
android :layout_below= "@+id/button"
android :layout_alignParentStart= "true" />
<Button
android :id= "@+id/button2"
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :text= "圆形进度条"
android :layout_below= "@+id/button1"
android :layout_alignParentStart= "true" />
<Button
android :id= "@+id/button3"
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :text= "条形进度条"
android :layout_below= "@+id/button2"
android :layout_alignParentStart= "true" />
<Button
android :id= "@+id/button4"
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :text= "警告信息"
android :layout_below= "@+id/button3"
android :layout_alignParentEnd= "true" />
<Button
android :id= "@+id/button5"
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :text= "进度对话框"
android :layout_below= "@+id/button4"
android :layout_alignParentEnd= "true" />
<EditText
android :id= "@+id/edit_text"
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :hint= "Type something here"
android :maxLines= "2"
android :layout_below= "@+id/button4"
android :layout_alignParentStart= "true"
android :layout_marginTop= "48dp" />
<ImageView
android :id= "@+id/image_view"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :gravity= "center"
android :src= "@mipmap/ic_launcher"
android :layout_below= "@+id/edit_text"
android :layout_alignParentStart= "true" />
//ImageView是用于在界面上展示图片的一个控件
<ProgressBar
android :id= "@+id/progress_bar"
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :layout_below= "@+id/button4"
android :layout_alignParentTop= "true" />
//默认的是圆形进度条
//ProgressBar用于在界面上显示一个进度条,表示程序正在加载一些数据
<ProgressBar
android :id= "@+id/progress_bar1"
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :layout_alignParentBottom= "true"
style= "?android:attr/progressBarStyleHorizontal"
android :max= "100" />//通过android:max属性给进度条设置一个最大值
//通过style属性来设置进度条类型,现在是条状的
</RelativeLayout>
Java——>MainActivity.java (3)
//——下面使用匿名类的方式来注册监听器
//public class MainActivity extends AppCompatActivity {
// private Button button;
//
// @Override
// protected void onCreate(Bundle savedInstanceState) {
// super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
// button =(Button) findViewById(R.id.button);
// button.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view) {
// //在此添加逻辑
// Toast.makeText(MainActivity.this, "You click Button", Toast.LENGTH_SHORT).show();
// }
// });
// }
//}
//——下面使用实现接口的方式来注册监听器
public class MainActivity extends Activity implements View.OnClickListener{
private Button button ;
private Button button1 ;
private Button button2 ;
private Button button3 ;
private Button button4 ;
private Button button5 ;
private EditText editText ; //通过点击按钮来获取EditText中输入的内容
private ImageView imageView ;
private ProgressBar progressBar ;
private ProgressBar progressBar1 ;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState) ;
setContentView(R.layout. activity_main) ;
button=(Button) findViewById(R.id. button) ;
button1=(Button) findViewById(R.id. button1) ;
button2=(Button) findViewById(R.id. button2) ;
button3=(Button) findViewById(R.id. button3) ;
button4=(Button) findViewById(R.id. button4) ;
button5=(Button) findViewById(R.id. button5) ;
editText = (EditText) findViewById(R.id. edit_text) ; //(通过findViewById()方法得到EditText的实例) 通过点击按钮来获取EditText中输入的内容
imageView =(ImageView) findViewById(R.id. image_view) ;
progressBar =(ProgressBar) findViewById(R.id. progress_bar) ;
progressBar1=(ProgressBar) findViewById(R.id. progress_bar1) ;
button.setOnClickListener( this) ;
button1.setOnClickListener( this) ;
button2.setOnClickListener( this) ;
button3.setOnClickListener( this) ;
button4.setOnClickListener( this) ;
button5.setOnClickListener( this) ;
}
@Override
public void onClick(View v){
switch (v.getId()){
case R.id. button:
//在此处添加逻辑
String inputText = editText.getText().toString() ;
//(在按钮点击事件里调用EditText的getText()方法获取到输入的内容,在调用toString()方法转换成字符串)
// 通过点击按钮来获取EditText中输入的内容
Toast. makeText(MainActivity. this, inputText , Toast. LENGTH_SHORT).show() ;
//(使用Toast将输入的内容显示出来)
// 通过点击按钮来获取EditText中输入的内容
break;
case R.id. button1: //将图片1转换为图片2
//在此处添加逻辑
imageView.setImageResource(R.drawable. ic_launcher) ; //调用setImageResource()方法将原来的图片改掉
break;
case R.id. button2: //效果:可看到控件在可见于不可见之间切换
//Android控件有三种属性:
// visible 控件可见(不指定android:visibility时,控件都是可见的)
//invisible 控件不可见,但是仍然占据着原来的位置和大小,就像是透明状态
//gone 控件不可见,并且不再占用任何屏幕空间
if ( progressBar.getVisibility() == View. GONE){ //通过getVisibility()方法来判断ProgressBar是否可见
progressBar.setVisibility(View. VISIBLE) ; //如果控件不可见,就将progressBar显示出来
}
else{
progressBar.setVisibility(View. GONE) ; //如果控件可见,就将progressBar隐藏起来
}
break;
case R.id. button3: //效果:一点点填满进度条
//在代码中动态的更改进度条的进度
int progress = progressBar1.getProgress() ;
progress = progress + 10 ;
progressBar1.setProgress(progress) ;
break;
case R.id. button4:
AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity. this) ; //通过AlertDialog.Builder创建出一个AlertDialog的实例
dialog.setTitle( "This is Dialog") ; //设置标题
dialog.setMessage( "Something important.") ; //设置内容
dialog.setCancelable( false) ; //设置可否取消属性
dialog.setPositiveButton( "OK" ,new DialogInterface.OnClickListener(){ //调用setPositiveButton()方法为对话框设置确定按钮的点击事件
@Override
public void onClick(DialogInterface dialog ,int which){
}
}) ;
dialog.setNegativeButton( "Cancel" ,new DialogInterface.OnClickListener(){ //setNegativeButton()方法为对话框设置取消按钮的点击事件
@Override
public void onClick(DialogInterface dialog ,int which){
}
}) ;
dialog.show() ; //通过调用show()方法将AlertDialog显示出来
break;
case R.id. button5:
ProgressDialog progressDialog = new ProgressDialog(MainActivity. this) ; //构建一个ProgressDialog对象
progressDialog.setTitle( "This is ProgressDialog") ; //设置标题
progressDialog.setMessage( "Loading") ; //设置内容
progressDialog.setCancelable( true) ; //设置可否取消属性
// (注意:如果设置为false,表示ProgressDialog是不能通过Back键取消的,一定要在代码中做好控制,
// 当数据加载完成后就必须调用ProgressDialog的dismiss()方法来关闭对话框,
// 否则ProgressDialog将会一直存在)
progressDialog.show() ; //通过调用show()方法将ProgressDialog显示出来
break;
default:
break;
}
}
}
//public class MainActivity extends AppCompatActivity {
// private Button button;
//
// @Override
// protected void onCreate(Bundle savedInstanceState) {
// super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
// button =(Button) findViewById(R.id.button);
// button.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view) {
// //在此添加逻辑
// Toast.makeText(MainActivity.this, "You click Button", Toast.LENGTH_SHORT).show();
// }
// });
// }
//}
//——下面使用实现接口的方式来注册监听器
public class MainActivity extends Activity implements View.OnClickListener{
private Button button ;
private Button button1 ;
private Button button2 ;
private Button button3 ;
private Button button4 ;
private Button button5 ;
private EditText editText ; //通过点击按钮来获取EditText中输入的内容
private ImageView imageView ;
private ProgressBar progressBar ;
private ProgressBar progressBar1 ;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState) ;
setContentView(R.layout. activity_main) ;
button=(Button) findViewById(R.id. button) ;
button1=(Button) findViewById(R.id. button1) ;
button2=(Button) findViewById(R.id. button2) ;
button3=(Button) findViewById(R.id. button3) ;
button4=(Button) findViewById(R.id. button4) ;
button5=(Button) findViewById(R.id. button5) ;
editText = (EditText) findViewById(R.id. edit_text) ; //(通过findViewById()方法得到EditText的实例) 通过点击按钮来获取EditText中输入的内容
imageView =(ImageView) findViewById(R.id. image_view) ;
progressBar =(ProgressBar) findViewById(R.id. progress_bar) ;
progressBar1=(ProgressBar) findViewById(R.id. progress_bar1) ;
button.setOnClickListener( this) ;
button1.setOnClickListener( this) ;
button2.setOnClickListener( this) ;
button3.setOnClickListener( this) ;
button4.setOnClickListener( this) ;
button5.setOnClickListener( this) ;
}
@Override
public void onClick(View v){
switch (v.getId()){
case R.id. button:
//在此处添加逻辑
String inputText = editText.getText().toString() ;
//(在按钮点击事件里调用EditText的getText()方法获取到输入的内容,在调用toString()方法转换成字符串)
// 通过点击按钮来获取EditText中输入的内容
Toast. makeText(MainActivity. this, inputText , Toast. LENGTH_SHORT).show() ;
//(使用Toast将输入的内容显示出来)
// 通过点击按钮来获取EditText中输入的内容
break;
case R.id. button1: //将图片1转换为图片2
//在此处添加逻辑
imageView.setImageResource(R.drawable. ic_launcher) ; //调用setImageResource()方法将原来的图片改掉
break;
case R.id. button2: //效果:可看到控件在可见于不可见之间切换
//Android控件有三种属性:
// visible 控件可见(不指定android:visibility时,控件都是可见的)
//invisible 控件不可见,但是仍然占据着原来的位置和大小,就像是透明状态
//gone 控件不可见,并且不再占用任何屏幕空间
if ( progressBar.getVisibility() == View. GONE){ //通过getVisibility()方法来判断ProgressBar是否可见
progressBar.setVisibility(View. VISIBLE) ; //如果控件不可见,就将progressBar显示出来
}
else{
progressBar.setVisibility(View. GONE) ; //如果控件可见,就将progressBar隐藏起来
}
break;
case R.id. button3: //效果:一点点填满进度条
//在代码中动态的更改进度条的进度
int progress = progressBar1.getProgress() ;
progress = progress + 10 ;
progressBar1.setProgress(progress) ;
break;
case R.id. button4:
AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity. this) ; //通过AlertDialog.Builder创建出一个AlertDialog的实例
dialog.setTitle( "This is Dialog") ; //设置标题
dialog.setMessage( "Something important.") ; //设置内容
dialog.setCancelable( false) ; //设置可否取消属性
dialog.setPositiveButton( "OK" ,new DialogInterface.OnClickListener(){ //调用setPositiveButton()方法为对话框设置确定按钮的点击事件
@Override
public void onClick(DialogInterface dialog ,int which){
}
}) ;
dialog.setNegativeButton( "Cancel" ,new DialogInterface.OnClickListener(){ //setNegativeButton()方法为对话框设置取消按钮的点击事件
@Override
public void onClick(DialogInterface dialog ,int which){
}
}) ;
dialog.show() ; //通过调用show()方法将AlertDialog显示出来
break;
case R.id. button5:
ProgressDialog progressDialog = new ProgressDialog(MainActivity. this) ; //构建一个ProgressDialog对象
progressDialog.setTitle( "This is ProgressDialog") ; //设置标题
progressDialog.setMessage( "Loading") ; //设置内容
progressDialog.setCancelable( true) ; //设置可否取消属性
// (注意:如果设置为false,表示ProgressDialog是不能通过Back键取消的,一定要在代码中做好控制,
// 当数据加载完成后就必须调用ProgressDialog的dismiss()方法来关闭对话框,
// 否则ProgressDialog将会一直存在)
progressDialog.show() ; //通过调用show()方法将ProgressDialog显示出来
break;
default:
break;
}
}
}
manifests——> AndroidManifest.xml(4)
<activity
android
:name=
".MainActivity"
>
<intent-filter>
<action android :name= "android.intent.action.MAIN" />
<category android :name= "android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<intent-filter>
<action android :name= "android.intent.action.MAIN" />
<category android :name= "android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
显示结果如下:
1.主界面
2.在文本框中输入内容,点击“显示文本框内容”,即可显示如下界面
3.点击“更换图片”,即可显示如下界面
4.点击“圆形进度条”,即可控制圆形进度的显示与否,显示如下界面
5.点击“条形进度条”,即可控制条形进度条的进度,显示如下界面
6.点击“警告信息”,即可显示如下界面
7.点击“进度对话框”,即可显示如下界面
//——下面使用匿名类的方式来注册监听器
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) ; //(通过findViewById()方法得到EditText的实例) 通过点击按钮来获取EditText中输入的内容
button.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View view) {
//在此添加逻辑
String inputText = editText.getText().toString() ;
//(在按钮点击事件里调用EditText的getText()方法获取到输入的内容,在调用toString()方法转换成字符串)
// 通过点击按钮来获取EditText中输入的内容
Toast. makeText(MainActivity. this, inputText , Toast. LENGTH_SHORT).show() ;
//(使用Toast将输入的内容显示出来)
// 通过点击按钮来获取EditText中输入的内容
}
}) ;
button2=(Button) findViewById(R.id. button2) ;
progressBar =(ProgressBar) findViewById(R.id. progress_bar) ;
button2.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View view) {
if ( progressBar.getVisibility() == View. GONE){ //通过getVisibility()方法来判断ProgressBar是否可见
progressBar.setVisibility(View. VISIBLE) ; //如果控件不可见,就将progressBar显示出来
}
else{
progressBar.setVisibility(View. GONE) ; //如果控件可见,就将progressBar隐藏起来
}
}
}) ;
}
}
super.onCreate(savedInstanceState) ;
setContentView(R.layout. activity_main) ;
button =(Button) findViewById(R.id. button) ;
editText = (EditText) findViewById(R.id. edit_text) ; //(通过findViewById()方法得到EditText的实例) 通过点击按钮来获取EditText中输入的内容
button.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View view) {
//在此添加逻辑
String inputText = editText.getText().toString() ;
//(在按钮点击事件里调用EditText的getText()方法获取到输入的内容,在调用toString()方法转换成字符串)
// 通过点击按钮来获取EditText中输入的内容
Toast. makeText(MainActivity. this, inputText , Toast. LENGTH_SHORT).show() ;
//(使用Toast将输入的内容显示出来)
// 通过点击按钮来获取EditText中输入的内容
}
}) ;
button2=(Button) findViewById(R.id. button2) ;
progressBar =(ProgressBar) findViewById(R.id. progress_bar) ;
button2.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View view) {
if ( progressBar.getVisibility() == View. GONE){ //通过getVisibility()方法来判断ProgressBar是否可见
progressBar.setVisibility(View. VISIBLE) ; //如果控件不可见,就将progressBar显示出来
}
else{
progressBar.setVisibility(View. GONE) ; //如果控件可见,就将progressBar隐藏起来
}
}
}) ;
}
}