第七周作业

1.三个界面,界面1点击按钮使用显式意图开启界面2.界面2点击按钮隐式意图开启界面3

2.在界面1做一个按钮开启浏览器访问百度

 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 tools:context=".MainActivity"
 7     >
 8 <Button
 9     android:id="@+id/bt1"
10     android:layout_width="wrap_content"
11     android:layout_height="wrap_content"
12     android:text="启动界面二"
13     />
14 
15 <Button
16     android:id="@+id/bt2"
17     android:onClick="click2"
18     android:layout_width="wrap_content"
19     android:layout_height="wrap_content"
20     android:text="打开百度"
21     android:layout_below="@id/bt1"
22     />
23 </RelativeLayout>
 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     tools:context=".SecondActivity">
 6     <Button
 7         android:id="@+id/bt2"
 8         android:layout_width="wrap_content"
 9         android:layout_height="wrap_content"
10         android:text="启动界面三"
11         />
12 
13 </RelativeLayout>
 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     tools:context=".ThirdActivity" >
 6 
 7     <TextView
 8         android:layout_width="wrap_content"
 9         android:layout_height="wrap_content"
10         android:text="Hello world!" />
11 
12 </RelativeLayout>
 1 package com.example.myapplication;
 2 
 3 import androidx.appcompat.app.AppCompatActivity;
 4 import android.os.Bundle;
 5 import android.view.View;
 6 import android.widget.Button;
 7 import android.content.Intent;
 8 import android.view.View.OnClickListener;
 9 import android.net.Uri;
10 
11 public class MainActivity extends AppCompatActivity {
12 
13     @Override
14     protected void onCreate(Bundle savedInstanceState) {
15         super.onCreate(savedInstanceState);
16         setContentView(R.layout.activity_main);
17         Button bt1=(Button)findViewById(R.id.bt1);
18         Button bt2=(Button)findViewById(R.id.bt2);
19 
20         bt1.setOnClickListener(new OnClickListener() {
21 
22             @Override
23             public void onClick(View v) {
24                 // TODO Auto-generated method stub
25                 Intent intent=new Intent(MainActivity.this,SecondActivity.class);
26                 startActivity(intent);
27             }
28         });
29 
30 
31     }
32     public void click2(View view){
33         Intent intent=new Intent();
34         intent.setAction("android.intent.action.VIEW");
35         intent.setData(Uri.parse("http://www.baidu.com"));
36         startActivity(intent);
37     }
38 }
 1 package com.example.myapplication;
 2 
 3 import androidx.appcompat.app.AppCompatActivity;
 4 import android.os.Bundle;
 5 import android.content.Intent;
 6 import android.view.View;
 7 import android.view.View.OnClickListener;
 8 import android.widget.Button;
 9 
10 public class SecondActivity extends AppCompatActivity {
11 
12     @Override
13     protected void onCreate(Bundle savedInstanceState) {
14         super.onCreate(savedInstanceState);
15         setContentView(R.layout.activity_second);
16         Button bt2=(Button)findViewById(R.id.bt2);
17         bt2.setOnClickListener(new OnClickListener() {
18 
19             @Override
20             public void onClick(View v) {
21                 // TODO Auto-generated method stub
22                 Intent intent=new Intent();
23                 intent.setAction("com.lrp.start");
24                 intent.addCategory("android.intent.category.DEFAULT");
25                 startActivity(intent);
26             }
27         });
28     }
29 }
 1 package com.example.myapplication;
 2 
 3 import androidx.appcompat.app.AppCompatActivity;
 4 import android.os.Bundle;
 5 
 6 public class ThirdActivity extends AppCompatActivity {
 7 
 8     @Override
 9     protected void onCreate(Bundle savedInstanceState) {
10         super.onCreate(savedInstanceState);
11         setContentView(R.layout.activity_third);
12     }
13 }

2.在界面1做一个按钮开启浏览器访问百度

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:app="http://schemas.android.com/apk/res-auto"
 4     xmlns:tools="http://schemas.android.com/tools"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     tools:context=".MainActivity"
 8     android:orientation="vertical">
 9 
10     <Button
11         android:id="@+id/bt_1"
12         android:layout_width="wrap_content"
13         android:layout_height="wrap_content"
14         android:text="开启访问百度"
15         android:textSize="30dp"
16         android:onClick="click1" />
17 
18 </LinearLayout>
 1 package com.example.myapplication103;
 2 import android.net.Uri;
 3 import android.os.Bundle;
 4 import android.app.Activity;
 5 import android.content.Intent;
 6 import android.util.Log;
 7 import android.view.Menu;
 8 import android.view.View;
 9 
10 public class MainActivity extends Activity {
11 
12     @Override
13     protected void onCreate(Bundle savedInstanceState) {
14         super.onCreate(savedInstanceState);
15         setContentView(R.layout.activity_main);
16         Log.e("MainActivity", "调用oncreate");
17     }
18 
19     @Override
20     protected void onStart() {
21         // TODO Auto-generated method stub
22         super.onStart();
23         Log.e("MainActivity", "调用onstart");
24     }
25     @Override
26     protected void onResume() {
27         // TODO Auto-generated method stub
28         super.onResume();
29         Log.e("MainActivity", "调用onresume");
30     }
31     @Override
32     protected void onPause() {
33         // TODO Auto-generated method stub
34         super.onPause();
35         Log.e("MainActivity", "调用onpause");
36     }
37 
38     @Override
39     protected void onStop() {
40         // TODO Auto-generated method stub
41         super.onStop();
42         Log.e("MainActivity", "调用onstop");
43     }
44 
45     @Override
46     protected void onDestroy() {
47         // TODO Auto-generated method stub
48         super.onDestroy();
49         Log.e("MainActivity", "调用ondestroy");
50     }
51 
52     @Override
53     protected void onRestart() {
54         // TODO Auto-generated method stub
55         super.onRestart();
56         Log.e("MainActivity", "调用onRestart");
57     }
58 
59     public void click1(View view){
60         Intent intent=new Intent();
61         intent.setAction("android.intent.action.VIEW");
62         intent.setData(Uri.parse("http://www.baidu.com"));
63         startActivity(intent);
64 
65     }
66 
67 }

 

3.2个edittext,4个按钮一个textview,实现简单计算器

 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:orientation="vertical"
 7     android:id="@+id/xb_1"
 8     tools:context=".Main4Activity">
 9 
10     <EditText
11         android:id="@+id/et_1"
12         android:layout_width="200dp"
13         android:layout_height="wrap_content"
14         android:layout_marginTop="100dp"
15         android:layout_marginLeft="100dp"
16         android:layout_marginRight="100dp"
17         android:inputType="number"
18         android:gravity="center"
19         android:textSize="20sp"/>
20 
21     <EditText
22         android:id="@+id/et_2"
23         android:layout_width="200dp"
24         android:layout_height="wrap_content"
25         android:layout_marginLeft="100dp"
26         android:layout_marginRight="100dp"
27         android:inputType="number"
28         android:gravity="center"
29         android:textSize="20sp"/>
30     <LinearLayout
31         android:id="@+id/xb_2"
32         android:layout_width="wrap_content"
33         android:layout_height="wrap_content"
34         android:layout_marginTop="20dp"
35         android:layout_marginLeft="20dp"
36         android:layout_marginRight="20dp"
37         android:orientation="horizontal">
38         <Button
39             android:id="@+id/btn_1"
40             android:layout_width="wrap_content"
41             android:layout_height="wrap_content"
42             android:text="加(+)"/>
43         <Button
44             android:id="@+id/btn_2"
45             android:layout_width="wrap_content"
46             android:layout_height="wrap_content"
47             android:text="减(-)"/>
48         <Button
49             android:id="@+id/btn_3"
50             android:layout_width="wrap_content"
51             android:layout_height="wrap_content"
52             android:text="乘(*)"/>
53          <Button
54              android:id="@+id/btn_4"
55             android:layout_width="wrap_content"
56             android:layout_height="wrap_content"
57             android:text="除(/)"/>
58     </LinearLayout>
59     <TextView
60         android:id="@+id/tv_1"
61         android:layout_width="wrap_content"
62         android:layout_height="wrap_content"
63         android:layout_marginTop="50dp"
64         android:layout_marginLeft="150dp"
65         android:layout_marginRight="150dp"
66         android:textSize="30sp"
67         android:gravity="center"
68         android:textColor="#06C6F7"
69         android:text="结果为"/>
70 
71 </LinearLayout>
 1 package com.example.week7;
 2 
 3 import androidx.appcompat.app.AppCompatActivity;
 4 import android.os.Bundle;
 5 import android.view.View;
 6 import android.widget.Button;
 7 import android.widget.EditText;
 8 import android.widget.TextView;
 9 import android.widget.Toast;
10 
11 public class Main4Activity extends AppCompatActivity {
12     private TextView tv_1;
13     private Button btn_1,btn_2,btn_3,btn_4;
14     private EditText et_1,et_2;
15 
16     @Override
17     protected void onCreate(Bundle savedInstanceState) {
18         super.onCreate(savedInstanceState);
19         setContentView(R.layout.activity_main4);
20         btn_1=(Button)this.findViewById(R.id.btn_1);
21         btn_2=(Button)findViewById(R.id.btn_2);
22         btn_3=(Button)findViewById(R.id.btn_3);
23         btn_4=(Button)findViewById(R.id.btn_4);
24         et_1=(EditText)this.findViewById(R.id.et_1);
25         et_2=(EditText)this.findViewById(R.id.et_2);
26         tv_1=(TextView)this.findViewById(R.id.tv_1);
27 
28         btn_1.setOnClickListener(new View.OnClickListener() {
29             @Override
30             public void onClick(View view) {
31                 String inputText1=et_1.getText().toString();
32                 String inputText2=et_2.getText().toString();
33                 float num1=Float.valueOf(inputText1).intValue();
34                 float num2=Float.valueOf(inputText2).intValue();
35                 num1=num1+num2;
36                 inputText1=String.valueOf(num1);
37                 tv_1.setText(inputText1);
38             }
39         });
40         btn_2.setOnClickListener(new View.OnClickListener() {
41             @Override
42             public void onClick(View view) {
43                 String inputText1=et_1.getText().toString();
44                 String inputText2=et_2.getText().toString();
45                 float num1=Float.valueOf(inputText1).intValue();
46                 float num2=Float.valueOf(inputText2).intValue();
47                 num1=num1-num2;
48                 inputText1=String.valueOf(num1);
49                 tv_1.setText(inputText1);
50             }
51         });
52         btn_3.setOnClickListener(new View.OnClickListener() {
53             @Override
54             public void onClick(View view) {
55                 String inputText1=et_1.getText().toString();
56                 String inputText2=et_2.getText().toString();
57                 float num1=Float.valueOf(inputText1).intValue();
58                 float num2=Float.valueOf(inputText2).intValue();
59                 num1=num1*num2;
60                 inputText1=String.valueOf(num1);
61                 tv_1.setText(inputText1);
62             }
63         });
64         btn_4.setOnClickListener(new View.OnClickListener() {
65             @Override
66             public void onClick(View view) {
67                 String inputText1=et_1.getText().toString();
68                 String inputText2=et_2.getText().toString();
69                 float num1=Float.valueOf(inputText1).intValue();
70                 float num2=Float.valueOf(inputText2).intValue();
71                 if (num2==0){
72                     Toast.makeText(Main4Activity.this,"除数不能为0",Toast.LENGTH_LONG).show();
73                 }else{
74                     num1=num1/num2;
75                     inputText1=String.valueOf(num1);
76                     tv_1.setText(inputText1);
77                 }
78             }
79         });
80     }
81 }

 

posted @ 2021-10-09 16:25  计算机1905geng  阅读(9)  评论(0编辑  收藏  举报