Android第三,七周作业
1.创建3个界面
第一个界面有3个button
第二个界面有单选按钮 学历:初中 高中 专科 本科
第三个界面有5个复选框 学过哪些课程
Java
Ios
Android
Html
Jsp
把第二个界面设置为启动界面
?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 android:orientation="vertical"> 8 9 <Button 10 android:id="@+id/btn_1" 11 android:layout_width="match_parent" 12 android:layout_height="60dp" 13 android:background="@drawable/btn" 14 android:text="按钮1" 15 android:textSize="24sp" 16 android:layout_margin="15dp" 17 android:onClick="click_1"/> 18 19 <Button 20 android:id="@+id/btn_2" 21 android:layout_width="match_parent" 22 android:layout_height="60dp" 23 android:background="@drawable/btn" 24 android:text="按钮2" 25 android:textSize="24sp" 26 android:layout_margin="15dp"/> 27 28 <Button 29 android:id="@+id/btn_3" 30 android:layout_width="match_parent" 31 android:layout_height="60dp" 32 android:background="@drawable/btn" 33 android:text="按钮3" 34 android:textSize="24sp" 35 android:layout_margin="15dp"/> 36 37 </LinearLayout>

<?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout 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 android:orientation="vertical"> 8 9 <TextView 10 android:id="@+id/t_1" 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" 13 android:text="你的学历是?" 14 android:textSize="28sp" 15 android:layout_margin="20dp"/> 16 17 18 <RadioGroup 19 android:id="@+id/rg_1" 20 android:layout_width="wrap_content" 21 android:layout_height="wrap_content" 22 android:layout_marginTop="60dp" 23 android:layout_marginLeft="20dp" 24 android:orientation="vertical"> 25 26 <RadioButton 27 android:id="@+id/rb_1" 28 android:layout_width="wrap_content" 29 android:layout_height="wrap_content" 30 android:text="初中" 31 android:textSize="24sp"/> 32 33 <RadioButton 34 android:id="@+id/rb_2" 35 android:layout_width="wrap_content" 36 android:layout_height="wrap_content" 37 android:text="高中" 38 android:textSize="24sp"/> 39 40 <RadioButton 41 android:id="@+id/rb_3" 42 android:layout_width="wrap_content" 43 android:layout_height="wrap_content" 44 android:text="专科" 45 android:textSize="24sp"/> 46 47 <RadioButton 48 android:id="@+id/rb_4" 49 android:layout_width="wrap_content" 50 android:layout_height="wrap_content" 51 android:text="本科" 52 android:textSize="24sp"/> 53 54 </RadioGroup> 55 56 </RelativeLayout>

<?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout 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 android:orientation="vertical" 8 android:padding="20dp"> 9 10 <TextView 11 android:id="@+id/t_1" 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content" 14 android:text="学过哪些课程?" 15 android:textSize="28sp"/> 16 17 <CheckBox 18 android:id="@+id/ck_1" 19 android:layout_width="wrap_content" 20 android:layout_height="wrap_content" 21 android:text="Java" 22 android:textSize="24sp" 23 android:layout_marginTop="40dp"/> 24 25 <CheckBox 26 android:id="@+id/ck_2" 27 android:layout_width="wrap_content" 28 android:layout_height="wrap_content" 29 android:text="IOS" 30 android:textSize="24sp" 31 android:layout_below="@id/ck_1"/> 32 33 <CheckBox 34 android:id="@+id/ck_3" 35 android:layout_width="wrap_content" 36 android:layout_height="wrap_content" 37 android:text="Android" 38 android:textSize="24sp" 39 android:layout_below="@id/ck_2"/> 40 41 <CheckBox 42 android:id="@+id/ck_4" 43 android:layout_width="wrap_content" 44 android:layout_height="wrap_content" 45 android:text="Html" 46 android:textSize="24sp" 47 android:layout_below="@id/ck_3"/> 48 49 <CheckBox 50 android:id="@+id/ck_5" 51 android:layout_width="wrap_content" 52 android:layout_height="wrap_content" 53 android:text="Jsp" 54 android:textSize="24sp" 55 android:layout_below="@id/ck_4"/> 56 57 </RelativeLayout>

2.在界面1上设置按钮点击事件
按钮1用onclick方式
按钮2和按钮3用监听方式
点击后用Toast弹出 按钮xxx被点击。
<?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 android:orientation="vertical"> 8 9 <Button 10 android:id="@+id/btn_1" 11 android:layout_width="match_parent" 12 android:layout_height="60dp" 13 android:background="@drawable/btn" 14 android:text="按钮1" 15 android:textSize="24sp" 16 android:layout_margin="15dp" 17 android:onClick="click_1"/> 18 19 <Button 20 android:id="@+id/btn_2" 21 android:layout_width="match_parent" 22 android:layout_height="60dp" 23 android:background="@drawable/btn" 24 android:text="按钮2" 25 android:textSize="24sp" 26 android:layout_margin="15dp"/> 27 28 <Button 29 android:id="@+id/btn_3" 30 android:layout_width="match_parent" 31 android:layout_height="60dp" 32 android:background="@drawable/btn" 33 android:text="按钮3" 34 android:textSize="24sp" 35 android:layout_margin="15dp"/> 36 37 </LinearLayout>

3.设计布局界面(详见QQ群)
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout 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 android:orientation="vertical"> 8 9 <ImageView 10 android:id="@+id/img_1" 11 android:layout_width="200dp" 12 android:layout_height="60dp" 13 android:background="@drawable/logo1" 14 android:layout_centerHorizontal="true" 15 android:layout_marginTop="40dp"/> 16 17 <EditText 18 android:id="@+id/t_1" 19 android:layout_width="300dp" 20 android:layout_height="60dp" 21 android:text="请输入手机号/邮箱" 22 android:textSize="22sp" 23 android:textColor="#65ADADAD" 24 android:gravity="center" 25 android:layout_centerHorizontal="true" 26 android:layout_marginTop="120dp"/> 27 28 <EditText 29 android:id="@+id/t_2" 30 android:layout_width="300dp" 31 android:layout_height="60dp" 32 android:text=" 请输入密码" 33 android:textSize="22sp" 34 android:textColor="#65ADADAD" 35 android:gravity="center" 36 android:layout_centerHorizontal="true" 37 android:layout_marginTop="180dp" 38 android:drawableRight="@drawable/yc"/> 39 40 <Button 41 android:id="@+id/btn_1" 42 android:layout_width="300dp" 43 android:layout_height="50dp" 44 android:background="@drawable/btn1" 45 android:text="登录" 46 android:textColor="#FFFFFF" 47 android:textSize="18sp" 48 android:layout_centerHorizontal="true" 49 android:layout_marginTop="270dp"/> 50 51 <Button 52 android:id="@+id/btn_2" 53 android:layout_width="300dp" 54 android:layout_height="50dp" 55 android:background="@drawable/btn2" 56 android:text="不注册,跳过登录" 57 android:textColor="#FFFFFF" 58 android:textSize="18sp" 59 android:layout_centerHorizontal="true" 60 android:layout_marginTop="330dp"/> 61 62 <TextView 63 android:id="@+id/t_3" 64 android:layout_width="wrap_content" 65 android:layout_height="wrap_content" 66 android:text="注册账号 忘记密码" 67 android:textSize="20sp" 68 android:textColor="#888888" 69 android:layout_centerHorizontal="true" 70 android:layout_marginTop="390dp"/> 71 72 <ImageView 73 android:id="@+id/img_2" 74 android:layout_width="45dp" 75 android:layout_height="45dp" 76 android:background="@drawable/wechat" 77 android:layout_marginLeft="80dp" 78 android:layout_marginTop="440dp"/> 79 80 <ImageView 81 android:id="@+id/img_3" 82 android:layout_width="45dp" 83 android:layout_height="45dp" 84 android:background="@drawable/apple" 85 android:layout_centerHorizontal="true" 86 android:layout_marginTop="440dp"/> 87 88 <ImageView 89 android:id="@+id/img_4" 90 android:layout_width="45dp" 91 android:layout_height="45dp" 92 android:background="@drawable/qq" 93 android:layout_marginLeft="285dp" 94 android:layout_marginTop="440dp"/> 95 96 <CheckBox 97 android:layout_width="wrap_content" 98 android:layout_height="wrap_content" 99 android:text="已阅读并同意 和 " 100 android:textSize="16sp" 101 android:textColor="#888888" 102 android:layout_centerHorizontal="true" 103 android:layout_marginTop="550dp"/> 104 105 <TextView 106 android:layout_width="wrap_content" 107 android:layout_height="wrap_content" 108 android:text="《用户协议》 《隐私政策》" 109 android:textSize="16sp" 110 android:textColor="#FF1A30" 111 android:layout_marginTop="555dp" 112 android:layout_marginLeft="168dp"/> 113 114 </RelativeLayout>

1.三个界面,界面1点击按钮使用显式意图开启界面2.
界面2点击按钮隐式意图开启界面3
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:id="@+id/bt_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="显式意图开启界面2"/> <Button android:id="@+id/bt_2" android:layout_below="@id/bt_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="隐式意图开启界面3"/> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".Main2Activity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" android:text="界面2"/> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".Main3Activity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" android:text="界面3"/> </LinearLayout>
package com.example.chap9; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn1=(Button)findViewById(R.id.bt_1); btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent=new Intent(MainActivity.this,Main2Activity.class); startActivity(intent); } }); }
package com.example.chap9; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class Main2Activity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); Button btn2=(Button)findViewById(R.id.bt_2); btn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent=new Intent(); intent.setAction("com.zqj.second"); startActivity(intent); } }); } }



2.在界面1做一个按钮开启浏览器访问百度
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:background="#94D0D8" android:orientation="vertical"> <Button android:id="@+id/bt_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="显式意图开启界面2" android:textColor="#000000" android:textSize="20sp" android:layout_margin="20dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/bt_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开启浏览器访问百度" android:textSize="20sp" android:textColor="#9C27B0" android:layout_margin="20dp" android:onClick="click3" android:padding="10dp" /> </LinearLayout>
package com.example.seventwo; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.Button; import com.example.seventwo.R; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn1=(Button)findViewById(R.id.bt_1); btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent=new Intent(MainActivity.this,MainActivity.class); startActivity(intent); } }); } public void click3(View view){ Intent intent=new Intent(); intent.setAction("android.intent.action.VIEW"); intent.setData(Uri.parse("http://www.baidu.com")); startActivity(intent); } }

3.2个edittext,4个按钮一个textview,实现简单计算器。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:layout_width="match_parent"> <TextView android:id="@+id/tv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="计算器" android:gravity="center" android:textSize="25dp" android:layout_margin="5dp"/> <EditText android:id="@+id/et_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入一个数" android:textSize="25dp" android:layout_marginTop="70dp"/> <EditText android:id="@+id/et_2" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入一个数" android:textSize="25dp" android:layout_below="@id/et_1"/> <Button android:id="@+id/b_1" android:layout_width="70dp" android:layout_height="50dp" android:text="+" android:textSize="25dp" android:background="#AAA898" android:layout_marginTop="200dp" android:layout_marginLeft="22dp"/> <Button android:id="@+id/b_2" android:layout_width="70dp" android:layout_height="50dp" android:text="-" android:textSize="25dp" android:background="#AAA898" android:layout_toRightOf="@id/b_1" android:layout_marginTop="200dp" android:layout_marginLeft="10dp"/> <Button android:id="@+id/b_3" android:layout_width="70dp" android:layout_height="50dp" android:text="*" android:textSize="25dp" android:background="#AAA898" android:layout_toRightOf="@id/b_2" android:layout_marginTop="200dp" android:layout_marginLeft="10dp"/> <Button android:id="@+id/b_4" android:layout_width="70dp" android:layout_height="50dp" android:text="/" android:textSize="25dp" android:background="#AAA898" android:layout_toRightOf="@id/b_3" android:layout_marginTop="200dp" android:layout_marginLeft="10dp"/> <TextView android:id="@+id/tv2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="25dp" android:text="结果是" android:layout_marginTop="300dp" android:layout_marginLeft="10dp"/> </RelativeLayout>
package com.example.chap11; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate( savedInstanceState ); setContentView( R.layout.activity_main ); findViewById( R.id.b_1 ).setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { String num1=((EditText)(findViewById( R.id.et_1 ))).getText().toString(); String num2=((EditText)(findViewById( R.id.et_2 ))).getText().toString(); int n1= Integer.parseInt( num1 ); int n2=Integer.parseInt( num2 ); int sum=n1+n2; TextView tv1=findViewById( R.id.tv2 ); tv1.setText( "结果是"+sum ); } } ); findViewById( R.id.b_2 ).setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { String num1=((EditText)(findViewById( R.id.et_1 ))).getText().toString(); String num2=((EditText)(findViewById( R.id.et_2 ))).getText().toString(); int n1=Integer.parseInt( num1 ); int n2=Integer.parseInt( num2 ); int sum=n1-n2; TextView tv1=findViewById( R.id.tv2 ); tv1.setText("结果是"+sum); } } ); findViewById( R.id.b_3 ).setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { String num1=((EditText)(findViewById( R.id.et_1 ))).getText().toString(); String num2=((EditText)(findViewById( R.id.et_2 ))).getText().toString(); int n1=Integer.parseInt( num1 ); int n2=Integer.parseInt( num2 ); int sum=n1*n2; TextView tv1=findViewById( R.id.tv2 ); tv1.setText("结果是"+sum); } } ); findViewById( R.id.b_4 ).setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { String num1=((EditText)(findViewById( R.id.et_1 ))).getText().toString(); String num2=((EditText)(findViewById( R.id.et_2 ))).getText().toString(); int n1=Integer.parseInt( num1 ); int n2=Integer.parseInt( num2 ); int sum=n1*n2; TextView tv1=findViewById( R.id.tv2 ); tv1.setText("结果是"+sum); } } ); findViewById( R.id.b_4).setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { String num1=((EditText)(findViewById( R.id.et_1 ))).getText().toString(); String num2=((EditText)(findViewById( R.id.et_2 ))).getText().toString(); int n1=Integer.parseInt( num1 ); int n2=Integer.parseInt( num2 ); int sum=n1/n2; TextView tv1=findViewById( R.id.tv2 ); tv1.setText("结果是"+sum); } } ); } }

浙公网安备 33010602011771号