1、实现随机生成一个6位的整数,并输出由该数的6位数字组成的最小数和最大数

layout文件:

 

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     tools:context="com.hanqi.testapp2.PractiseActivity2"
11     android:orientation="vertical">
12 
13     <TextView
14         android:layout_width="match_parent"
15         android:layout_height="80dp"
16         android:id="@+id/tv1"/>
17     <Button
18         android:layout_width="wrap_content"
19         android:layout_height="wrap_content"
20         android:id="@+id/btt"
21         android:text="随机生成"
22         android:onClick="btt_onClick"/>
23 </LinearLayout>

 

java类代码:

 1 package com.hanqi.testapp2;
 2 
 3 import android.os.Bundle;
 4 import android.support.v7.app.AppCompatActivity;
 5 import android.view.View;
 6 import android.widget.Button;
 7 import android.widget.TextView;
 8 
 9 import java.util.Arrays;
10 
11 public class PractiseActivity2 extends AppCompatActivity {
12 
13     TextView tv1;
14     Button btt;
15     @Override
16     protected void onCreate(Bundle savedInstanceState) {
17         super.onCreate(savedInstanceState);
18         setContentView(R.layout.activity_practise2);
19         tv1 = (TextView)findViewById(R.id.tv1);
20         btt = (Button)findViewById(R.id.btt);
21     }
22     public void btt_onClick(View v)
23     {
24         //根据数组个数循环
25         int[] ary = new int[6];
26         String result = "";
27         int max = 0;
28         int min = 9;
29 
30         for(int i = 0; i < 6; i++){
31             ary[i] = (int) (Math.random()*10);
32             //首位数字不能为0
33             while(ary[0] == 0)
34             {
35                 ary[0] = (int) (Math.random()*10);
36             }
37             result += ary[i];
38             //取出最大最小值,用于组装最大最小值
39             max = Math.max(ary[i], max);
40             min = Math.min(ary[i], min);
41         }
42 
43         Arrays.sort(ary);
44         //最大值
45         String maxResult = "";
46         for(int i = 5; i >= 0; i--){
47             maxResult += ary[i];
48         }
49         //最小值
50         String minResult = "";
51         for(int i : ary){
52             minResult += i;
53         }
54         tv1.setText(result+"\n"+minResult+"\n"+maxResult);
55     }
56 }

效果为:

2、写出实现如图所示的对话框的代码。

layout文件:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     tools:context="com.hanqi.testapp2.PractiseActivity3">
11 
12     <Button
13         android:layout_width="wrap_content"
14         android:layout_height="wrap_content"
15         android:onClick="bt_1_onClick"
16         android:text="提示"/>
17 </RelativeLayout>

java类代码:

 1 package com.hanqi.testapp2;
 2 
 3 import android.app.AlertDialog;
 4 import android.support.v7.app.AppCompatActivity;
 5 import android.os.Bundle;
 6 import android.view.View;
 7 
 8 public class PractiseActivity3 extends AppCompatActivity {
 9 
10     @Override
11     protected void onCreate(Bundle savedInstanceState) {
12         super.onCreate(savedInstanceState);
13         setContentView(R.layout.activity_practise3);
14     }
15     public void bt_1_onClick(View v)
16     {
17         new AlertDialog.Builder(this)
18                 .setTitle("提示")
19                 .setMessage("确定要删除吗?\n要删除,请点击“是”。")
20                 .setPositiveButton("否",null)
21                 .setNegativeButton("是",null)
22                 .show();
23     }
24 }

效果图:

3、设计界面如下图如示,在编辑框中只接受电话号码,实现“拨打电话”和“发送短信”的功能。

layout文件:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     tools:context="com.hanqi.testapp2.PractiseActivity3"
11     android:orientation="vertical">
12 
13    <TextView
14        android:layout_width="match_parent"
15        android:layout_height="wrap_content"
16        android:text="请输入电话号码"/>
17     <EditText
18         android:layout_width="match_parent"
19         android:layout_height="wrap_content"
20         android:inputType="phone"
21         android:id="@+id/et1_phone"/>
22     <LinearLayout
23         android:layout_width="match_parent"
24         android:layout_height="wrap_content">
25         <Button
26             android:layout_width="0dp"
27             android:layout_height="wrap_content"
28             android:layout_weight="1"
29             android:text="拨出此号码"
30             android:onClick="bt_bd_onClick"/>
31         <Button
32             android:layout_width="0dp"
33             android:layout_height="wrap_content"
34             android:layout_weight="1"
35             android:text="向此号码发送短信"
36             android:onClick="bt_fs_onClick"/>
37     </LinearLayout>
38 </LinearLayout>

java类代码:

 1 package com.hanqi.testapp2;
 2 
 3 import android.content.Intent;
 4 import android.net.Uri;
 5 import android.os.Bundle;
 6 import android.support.v7.app.AppCompatActivity;
 7 import android.view.View;
 8 import android.widget.EditText;
 9 import android.widget.Toast;
10 
11 public class PractiseActivity3 extends AppCompatActivity {
12 
13     EditText et1_phone;
14     @Override
15     protected void onCreate(Bundle savedInstanceState) {
16         super.onCreate(savedInstanceState);
17         setContentView(R.layout.activity_practise3);
18         et1_phone = (EditText)findViewById(R.id.et1_phone);
19     }
20     public void bt_bd_onClick(View v)
21     {
22         String phone = getPhone();
23         if (phone == null) return;
24         Intent intent = new Intent(Intent.ACTION_DIAL);
25         Uri uri = Uri.parse("tel:"+phone);
26         intent.setData(uri);
27         startActivity(intent);
28     }
29     public String getPhone()
30     {
31         String phone = et1_phone.getText().toString().trim();
32         if (phone.length()==0)
33         {
34             Toast.makeText(PractiseActivity3.this, "请正确填写电话号码", Toast.LENGTH_SHORT).show();
35             return null;
36         }
37         return phone;
38     }
39     public String getMessage()
40     {
41         String message = et1_phone.getText().toString().trim();
42         if (message.length()==0)
43         {
44             Toast.makeText(PractiseActivity3.this, "请正确填写电话号码", Toast.LENGTH_SHORT).show();
45             return null;
46         }
47         return message;
48     }
49     public void bt_fs_onClick(View v)
50     {
51         String message = getMessage();
52         if (message == null) return;
53         Uri smsToUri = Uri.parse("smsto:message");
54         Intent intent = new Intent(Intent.ACTION_SENDTO, smsToUri);
55         intent.setData(Uri.parse("smsto:"+message));
56         startActivity(intent);
57     }
58 }

效果图为: