安卓开发起步(四)

交互设计

  2020.10.02 本文是学习过程记录,这一篇主要包含意图的更多用法和布局的更多使用。 小米手机真机测试 AndroidStudio小米手机连接设置的主要步骤: 设置中找到【关于/全部参数】然后找到MIUI的版本,连续点很多次,打开开发者模式回到设置中找到【更多设置/开发者模式】然后打开【USB调试】以及允许【USB安装】等相关授权连线后选择【传输文件】,不要选充电或传输图片华为手机据说还要安装单独的手机驱动,没有亲测。 页面跳转 在前面一篇介绍了页面跳转的方法: 创建一个目标页面。当前页面的java代码中要含有一个用来执行跳转的方法。这个方法中创建一个意图Intent,并设定意图的目标页面。当前页界面上的按钮绑定onClick属性到同名函数。如果我们需要从当前页面向目标页面传递数据,那么只要把数据指定给意图Intent。目标页面是可以访问这个Intent的,也能从接收的Intent中提取到数据。 下面是一个示例代码。 MainActitivity.java中新增代码: public void goTargetPage(View view){ Intent mi=new Intent(this,TargetActivity.class); mi.putExtra("data","Hello"); startActivity(mi);}activity_main.xml新增代码: <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="148dp" android:layout_marginTop="248dp" android:text="进入目标页面" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" android:onClick="goTargetPage"/>新建TargetActivity的java代码主体部分: protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_target); Intent mi=getIntent(); String msg=mi.getStringExtra("data"); Toast.makeText(this,msg,Toast.LENGTH_LONG).show();}上面代码Toast是在手机屏幕底部弹出小提示。 ##打开浏览器网页 利用Intent可以拉起外部浏览器打开特定网页。代码示意如下。 MainActivity.java中新增代码,然后把这个方法绑定到按钮即可: public void openUri(View v){ Uri uri=Uri.parse("http://www.10knet.com/"); Intent mi=new Intent(Intent.ACTION_VIEW,uri); startActivity(mi);}##打开照相机 利用Intent实现打开照相机,让用户拍照,然后把照片返回给Activity。 MainActivity.java代码新增部分。 public void openCam(View v){ Intent mi=new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(mi,9);}@Overrideprotected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode==9 && resultCode==RESULT_OK){ Bitmap bmp=(Bitmap) data.getExtras().get("data"); ImageView miv=findViewById(R.id.camImageView); miv.setImageBitmap(bmp); }}注意这里使用的是startActivityForResult,它只是临时性的调用相机,调用之后用户按返回按钮并不会回到相机界面。与之对应的是下面的监听方法onActivityResult,它实际上一直检测是否有Intent被完成,这里实际编写不需要输入Override…这些,直接输入onActivity…就可以从自动提示中选择完成了。 注意requestCode==9中间的9和上面的startActivityForResult(mi,9);对应,任意数字都行。另外注意&& resultCode==RESULT_OK这个表示拍照成功。 Bitmap bmp=(Bitmap) data.getExtras().get("data");以及下面的三句是把拍照图片填充到ImageView界面元素中,运行效果如下图。 与这个对应的activity_main.xml文件中的界面代码如下所示,可供参考。 <ImageView android:id="@+id/camImageView" android:layout_width="180dp" android:layout_height="240dp" android:layout_marginStart="120dp" android:layout_marginTop="20dp" android:layout_marginEnd="120dp" android:background="#EEE" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/button" tools:ignore="MissingConstraints" /><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="125dp" android:layout_marginTop="404dp" android:layout_marginEnd="125dp" android:onClick="openCam" android:text="打开照相机获取拍照" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="1.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />##LinearLayout AndroidStudio默认总是使用约束布局ConstraintLayout,如果改为使用线性布局LinearLayout可能更加简便。下面线性布局代码仅供参考。 <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:text="BTN01" /> <Button android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:text="BTN02" /> <Button android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:text="BTN03" /></LinearLayout><LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_marginLeft="50dp" android:layout_marginRight="50dp"> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="BTN01" /> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="BTN02" /> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="BTN03" /></LinearLayout>以上代码生成的结果如下所示。 上面代码中,orientation方向是子元素横向排列还是竖向堆叠。match_parent是充满父层;wrap_content是被子层撑满。layout_weight会产生大小比例影响。 相对布局 相对布局RelativeLayout是相对于其他元素进行定位的。比如下面代码: <ImageView android:id="@+id/centerIV" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_margin="10dp" android:src="@mipmap/ic_launcher" /><ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentRight="true" android:src="@mipmap/ic_launcher" /><ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/centerIV" android:layout_alignLeft="@+id/centerIV" android:src="@mipmap/ic_launcher" /><ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/centerIV" android:layout_toRightOf="@+id/centerIV" android:src="@mipmap/ic_launcher" />得到如下效果: 注意上面代码中的内容: layout_centerInParent是横竖都居中;layout_above和后面出现的"@+id/centerIV",这个和开头的android:id="@+id/centerIV"对应。layout_above是在某个元素之上,对应的还有bottom,alignTop等等。数据类型转换 从界面上提取到的用户输入虽然是字符串,但还要再toStrring()之后才能用,字符串转双精度小数可以用Double.parseDouble(str)。 下面是计算BMI体脂指数的小案例代码Activity.java部分。 public void calc(View v){ EditText weightET=findViewById(R.id.weight); EditText heightET=findViewById(R.id.height); String wei=weightET.getText().toString(); String hei=heightET.getText().toString(); Double we=Double.parseDouble(wei); Double he=Double.parseDouble(hei); Double bmi=we/(he*he); Toast.makeText(this,bmi.toString(),Toast.LENGTH_LONG).show();}下面是界面代码部分: <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="50dp" android:orientation="vertical"> <EditText android:id="@+id/weight" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入体重公斤数" /> <EditText android:id="@+id/height" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入身高米数" /> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:onClick="calc" android:text="计算" /></LinearLayout>界面效果如下图所示: 其他小技巧 如下图所示,点击这个小c可以打开对应的java文件。 如下图示,点击这个小图标可以快速打开对应的xml界面文件。 使用背景图片:先把图片拖拽到【res/drawable】文件夹下,然后就可以android:background="@drawable/timg"使用,注意这里不要带文件后缀。 对于红色文字的未导入的包,可以鼠标点上去,然后Alt+Enter自动补全。代码自动格式化快捷键是【Ctrl+Shift+Alt+L】未完待续。 欢迎批评指正,交流学习。

标签: 交互设计