xlm是什么格式,xlmi

  

  2020.10.02   

  

  这篇文章是学习过程的记录。这篇文章主要包含更多的意图和布局的用法。   

  

  用真正的小米手机测试AndroidStudio小米手机连接设置的主要步骤:   

  

  在设置里找到【关于/所有参数】,然后找到MIUI版本。连续点击多次。打开开发者模式,回到设置找到【更多设置/开发者模式】。然后开启[USB调试]并允许[USB安装]等相关授权连接,再选择[传输文件]。不收费,不转让图片。华为的手机据说要安装单独的手机驱动。没有亲测。   

  

  页面跳转在上一篇文章中介绍了页面跳转的方法:   

  

  创建目标页面。当前页面的java代码应该包含一个执行跳转的方法。该方法创建一个意图,并设置该意图的目标页面。当前页面界面上的按钮将onClick属性绑定到同名的函数。如果我们需要将数据从当前页面转移到目标页面,那么只需将数据指定到目的页面即可。目标页面可以访问该意图,并从接收到的意图中提取数据。   

  

  下面是一个示例代码。   

  

  MainActitivity.java中的新代码:   

  

  public void goTargetPage(View View){ Intent mi=new Intent(this,target activity . class);mi.putExtra('data ',' Hello ');start activity(mi);}activity_main.xml新代码:   

  

  button Android : layout _ width=' wrap _ content ' Android : layout _ height=' wrap _ content ' Android : layout _ margin start=' 148 DP ' Android : layout _ margin Ntp=' 248 DP ' Android : text='转到目标页面' app : layout _ constraint start _ to start of=' parent ' app 3360 layout _ constraint stop _ totopo   

  

  protected void onCreate(Bundle savedInstanceState){ super . onCreate(savedInstanceState);setContentView(r . layout . activity _ target);intent mi=getIntent();string msg=mi . getstring extra(' data ');Toast.makeText(这个,味精,吐司。长度_长)。show();}上面的代码Toast是在手机屏幕下方弹出的一个小提示。   

  

  # #打开浏览器网页   

  

  Intent可用于打开外部浏览器以打开特定网页。代码如下所示。   

  

  MainActivity.java,然后将此方法绑定到按钮:   

  

  public void open Uri(View v){ Uri Uri=Uri . parse(' http://www . 10k net . com/');意向mi=新意向(意向。ACTION_VIEW,uri);start activity(mi);} # # #打开摄像头   

  

  有意向的打开相机,让用户拍照,然后将照片返回Activity。   

  

  MainActivity.java电码的新部分。   

  

  public void openCam(View v){ Intent mi=new Intent(MediaStore。动作_图像_捕捉);startActivityForResult(mi,9);} @ override protected void on activity result(int request code,int resultCode,@ Nullable Intent data){ super . on activity result(request code,resultCode,data);if(request code==9 RESULT code==RESULT _ OK){ Bitmap BMP=(Bitmap)data . get extras()。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】未完待续。
欢迎批评指正,交流学习。

相关文章