教材:第一行代码(第2版)
新建项目UIWidgetTest:
TextView
//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\"><TextViewandroid:id=\"@+id/text_view\"android:layout_width=\"match_parent\"android:layout_height=\"wrap_content\"android:gravity=\"center\"android:textSize=\"24sp\"android:textColor=\"#ff9933\"android:text=\"This is TextView\" /></LinearLayout>
match_parent 大小与父布局相同wrap_parent 大小刚好包含控件内容android:gravity指定文字对齐方式;可选值有top、bottom、left、right、center等;可以用“|”来指定多个值,例如 center == center_vertical|center_horizontal ,即文字在水平和垂直方向都居中对齐;textSize:字体大小textColor:字体颜色
颜色代码大全
Button
//activity_main.xml 添加代码<Buttonandroid:id=\"@+id/button\"android:layout_width=\"match_parent\"android:layout_height=\"wrap_content\"android:text=\"Button\"android:background=\"#ffff00\"android:textAllCaps=\"false\" />//按钮文字默认全部大写,利用上行代码关闭//background可设置按钮背景
//MainActivity.java//匿名类方式:为Button点击事件注册一个监听器public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button button = (Button) findViewById(R.id.button);button.setOnClickListener(new View.OnClickListener(){@Overridepublic void onClick(View v){//逻辑}});}}//实现接口方式:为Button点击事件注册一个监听器public class MainActivity extends AppCompatActivity implements View.OnClickListener{@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button button = (Button) findViewById(R.id.button);button.setOnClickListener(this);}@Overridepublic void onClick(View v){switch (v.getId()){case R.id.button://逻辑break;default:break;}}}
EditText
//activity_main.xml 添加代码<EditTextandroid:id=\"@+id/edit_text\"android:layout_width=\"match_parent\"android:layout_height=\"wrap_content\"android:hint=\"Type something here\"android:maxLines=\"2\" />
android:hint设置输入框内的提示性文本;android:maxLines设置输入框的最大行数;因为输入框的高度为适应控件大小,如果不设限制输入文本过长时输入框就会无限扩大。
//MainActivity.java//结合使用Button和EditTextpublic class MainActivity extends AppCompatActivity implements View.OnClickListener{private EditText editText; //注意这里@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button button = (Button) findViewById(R.id.button);editText = (EditText) findViewById(R.id.edit_text); //添加button.setOnClickListener(this);}@Overridepublic void onClick(View v){switch (v.getId()){case R.id.button:String inputText = editText.getText().toString(); //获取输入的内容,转化成字符串Toast.makeText(MainActivity.this,inputText,Toast.LENGTH_SHORT).show(); //利用Toast显示break;default:break;}}}
ImageView
在res文件夹下新建文件夹drawable-xhdpi,(xhdpi指分辨率)
准备好两张图片img_1.png与img_2.png放在该文件夹中;
//activity_main.xml 添加代码<ImageViewandroid:id=\"@+id/image_view\"android:layout_width=\"wrap_content\"android:layout_height=\"wrap_content\"android:src=\"@drawable/img_1\" />
动态地更改图片:
//MainActivity.java修改//添加private ImageView imageView;//添加imageView = (ImageView) findViewById(R.id.image_view);//修改按钮事件case R.id.button:imageView.setImageResource(R.drawable.img_2); //点击按钮,图片由1改为2break;
ProgressBar
//activity_main.xml 添加代码//上图的红色旋转进度条<ProgressBarandroid:id=\"@+id/progress_bar\"android:layout_width=\"match_parent\"android:layout_height=\"wrap_content\"/>
如何让进度条在数据加载完消失?
控件的可见属性:1、设置方法一:android:visibility可选值:visible(默认)invisible(不可见但处于透明状态)gone(完全消失,不占任何屏幕空间)2、设置方法二:setVisibility()方法View.VISIBLEView.INVISIBLEView.GONE
//MainActivity.java修改//添加private ProgressBar progressBar;//添加progressBar = (ProgressBar) findViewById(R.id.progress_bar);//修改按钮事件case R.id.button:if(progressBar.getVisibility() == View.GONE){progressBar.setVisibility(View.VISIBLE);}else{progressBar.setVisibility(View.GONE);}break; //点击按钮,进度条会在显示和隐藏中来回切换
给进度条指定不同样式
//activity_main.xml<ProgressBarandroid:id=\"@+id/progress_bar\"android:layout_width=\"match_parent\"android:layout_height=\"wrap_content\"style=\"?android:attr/progressBarStyleHorizontal\" //水平进度条android:max=\"100\" //设置进度条最大值/>
//MainActivity.java修改//按钮事件case R.id.button:int progress = progressBar.getProgress();progress = progress + 10;progressBar.setProgress(progress);break; //每点击一次按钮,水平进度条加10
AlertDialog
弹出一个对话框,
这个对话框置顶于所有界面元素之上,
能够屏蔽掉其他控件的交互能力,
因此一般用于提示一些非常重要的内容或警告信息。
//MainActivity.java修改//按钮事件case R.id.button:AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);dialog.setTitle(\"This is Dialog\"); //设置标题dialog.setMessage(\"Something important!\"); //设置内容dialog.setCancelable(false); //该对话框是否可以取消(true:点击屏幕其他位置或back键即可取消)dialog.setPositiveButton(\"OK\", new DialogInterface.OnClickListener() { //确定按钮点击事件@Overridepublic void onClick(DialogInterface dialogInterface, int which) {}});dialog.setNegativeButton(\"Cancel\", new DialogInterface.OnClickListener() { //取消按钮点击事件@Overridepublic void onClick(DialogInterface dialogInterface, int which) {}});dialog.show();break; //每点击一次按钮,水平进度条加10
ProgressDialog
与AlertDialog类似,
差别在于P对话框会显示一个进度条,
一般用于表示当前操作比较耗时,
让用户耐心等待。
//MainActivity.java修改//按钮事件case R.id.button:ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);progressDialog.setTitle(\"This is ProgressDialog\");progressDialog.setMessage(\"Loading...\");progressDialog.setCancelable(true);progressDialog.show();break; //每点击一次按钮,水平进度条加10
总结
<LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"android:orientation=\"vertical\"android:layout_width=\"match_parent\"android:layout_height=\"match_parent\"><TextViewandroid:id=\"@+id/text_view\"android:layout_width=\"match_parent\"android:layout_height=\"wrap_content\"android:gravity=\"center\"android:textSize=\"24sp\"android:textColor=\"#ff9933\"android:text=\"This is TextView\" /><Buttonandroid:id=\"@+id/button\"android:layout_width=\"match_parent\"android:layout_height=\"wrap_content\"android:text=\"Button\"android:background=\"#ffff00\"android:textAllCaps=\"false\" /><EditTextandroid:id=\"@+id/edit_text\"android:layout_width=\"match_parent\"android:layout_height=\"wrap_content\"android:hint=\"Type something here\"android:maxLines=\"2\" /><ImageViewandroid:id=\"@+id/image_view\"android:layout_width=\"wrap_content\"android:layout_height=\"wrap_content\"android:src=\"@drawable/img_1\" /><ProgressBarandroid:id=\"@+id/progress_bar\"android:layout_width=\"match_parent\"android:layout_height=\"wrap_content\"style=\"?android:attr/progressBarStyleHorizontal\"android:max=\"100\"/></LinearLayout>
package com.example.uiwidgettest;import androidx.appcompat.app.AlertDialog;import androidx.appcompat.app.AppCompatActivity;import android.app.ProgressDialog;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 AppCompatActivity implements View.OnClickListener {private EditText editText;private ImageView imageView;private ProgressBar progressBar;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button button = (Button) findViewById(R.id.button);editText = (EditText) findViewById(R.id.edit_text);imageView = (ImageView) findViewById(R.id.image_view);progressBar = (ProgressBar) findViewById(R.id.progress_bar);button.setOnClickListener(this);}@Overridepublic void onClick(View v){switch (v.getId()){case R.id.button:// String inputText = editText.getText().toString(); //获取输入的内容,转化成字符串// Toast.makeText(MainActivity.this,inputText, Toast.LENGTH_SHORT).show(); //利用Toast显示// imageView.setImageResource(R.drawable.img_2);// if(progressBar.getVisibility() == View.GONE){// progressBar.setVisibility(View.VISIBLE);// }else{// progressBar.setVisibility(View.GONE);// }// int progress = progressBar.getProgress();// progress = progress + 10;// progressBar.setProgress(progress);// AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);// dialog.setTitle(\"This is Dialog\");// dialog.setMessage(\"Something important!\");// dialog.setCancelable(false);// dialog.setPositiveButton(\"OK\", new DialogInterface.OnClickListener() {// @Override// public void onClick(DialogInterface dialogInterface, int which) {// }// });// dialog.setNegativeButton(\"Cancel\", new DialogInterface.OnClickListener() {// @Override// public void onClick(DialogInterface dialogInterface, int which) {// }// });// dialog.show();ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);progressDialog.setTitle(\"This is ProgressDialog\");progressDialog.setMessage(\"Loading...\");progressDialog.setCancelable(true);progressDialog.show();break;default:break;}}}