Android第七周作业
1.三个界面,界面1点击按钮使用显式意图开启界面2.
   界面2点击按钮隐式意图开启界面3
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 android:orientation="vertical" 8 tools:context=".MainActivity"> 9 10 <Button 11 android:id="@+id/bt_1" 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content" 14 android:background="#F2B4FC" 15 android:text="显示意图开启界面2" 16 android:textSize="20sp" 17 android:textColor="#000000" 18 android:layout_marginTop="20dp" 19 android:layout_marginLeft="10dp"/> 20 21 </LinearLayout>
1 package com.example.homework11; 2 3 import androidx.appcompat.app.AppCompatActivity; 4 5 import android.content.Intent; 6 import android.os.Bundle; 7 import android.view.View; 8 import android.widget.Button; 9 10 public class MainActivity extends AppCompatActivity { 11 12 @Override 13 protected void onCreate(Bundle savedInstanceState) { 14 super.onCreate(savedInstanceState); 15 setContentView(R.layout.activity_main); 16 Button btn1=(Button)findViewById(R.id.bt_1); 17 btn1.setOnClickListener(new View.OnClickListener() { 18 @Override 19 public void onClick(View view) { 20 Intent intent=new Intent(MainActivity.this,Main2Activity.class); 21 startActivity(intent); 22 } 23 }); 24 } 25 }
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:app="http://schemas.android.com/apk/res-auto" 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 tools:context=".Main2Activity"> 8 9 <Button 10 android:id="@+id/bt_2" 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" 13 android:background="#EEA6BF" 14 android:text="隐式意图开启界面3" 15 android:textSize="20sp" 16 android:textColor="#000000" 17 android:layout_marginLeft="10dp" 18 android:layout_marginTop="20dp"/> 19 20 </LinearLayout>
1 package com.example.homework11; 2 3 import androidx.appcompat.app.AppCompatActivity; 4 5 import android.content.Intent; 6 import android.os.Bundle; 7 import android.view.View; 8 import android.widget.Button; 9 10 public class Main2Activity extends AppCompatActivity { 11 12 @Override 13 protected void onCreate(Bundle savedInstanceState) { 14 super.onCreate(savedInstanceState); 15 setContentView(R.layout.activity_main2); 16 Button btn2=(Button)findViewById(R.id.bt_2); 17 btn2.setOnClickListener(new View.OnClickListener() { 18 @Override 19 public void onClick(View view) { 20 Intent intent=new Intent(); 21 intent.setAction("com.ym.second"); 22 startActivity(intent); 23 } 24 }); 25 } 26 }
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:background="#A5D4FA" 8 tools:context=".Main3Activity"> 9 10 <TextView 11 android:id="@+id/tv_1" 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content" 14 android:text="界面3" 15 android:textSize="20sp" 16 android:layout_centerInParent="true" 17 android:textColor="#000000"/> 18 19 </RelativeLayout>
1 <activity android:name=".Main3Activity"> 2 <intent-filter> 3 <action android:name="com.ym.second" /> 4 5 <category android:name="android.intent.category.DEFAULT" /> 6 </intent-filter> 7 </activity> 8 <activity android:name=".Main2Activity" > 9 </activity> 10 <activity android:name=".MainActivity"> 11 <intent-filter> 12 <action android:name="android.intent.action.MAIN" /> 13 14 <category android:name="android.intent.category.LAUNCHER" /> 15 </intent-filter> 16 </activity>

 
 
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 <Button 9 android:id="@+id/bt_1" 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:background="#77DDEB" 13 android:text="显式意图开启界面2" 14 android:textSize="20sp" 15 android:layout_marginTop="20dp" 16 android:layout_marginLeft="20dp"/> 17 <Button 18 android:id="@+id/bt_2" 19 android:layout_width="wrap_content" 20 android:layout_height="wrap_content" 21 android:background="#BE36E4" 22 android:text="开启浏览器访问百度" 23 android:textSize="20sp" 24 android:layout_marginTop="20dp" 25 android:layout_marginLeft="20dp" 26 android:padding="10dp" 27 android:onClick="click1"/> 28 29 30 </LinearLayout>
1 package com.example.chap10; 2 3 import androidx.appcompat.app.AppCompatActivity; 4 5 import android.content.Intent; 6 import android.net.Uri; 7 import android.os.Bundle; 8 import android.view.View; 9 import android.widget.Button; 10 11 12 public class MainActivity extends AppCompatActivity { 13 14 @Override 15 protected void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.activity_main); 18 Button btn1=(Button)findViewById(R.id.bt_1); 19 btn1.setOnClickListener(new View.OnClickListener() { 20 @Override 21 public void onClick(View view) { 22 Intent intent=new Intent(MainActivity.this,Main2Activity.class); 23 startActivity(intent); 24 } 25 }); 26 } 27 public void click1(View view){ 28 Intent intent=new Intent(); 29 intent.setAction("android.intent.action.VIEW"); 30 intent.setData(Uri.parse("http://www.baidu.com")); 31 startActivity(intent); 32 } 33 }

3.2个edittext,4个按钮一个textview,实现简单计算器。
提示1:如何获取edittext上的数据?
String num1=((EditText)(findViewById(R.id.et1))).getText().toString();//获取et1上面的文本,并
转成字符串
提示2:字符串如何转int
int n1=Integer.parseInt(num1);
提示3:如何把计算结果显示在textview上?
TextView tv1=(TextView)findViewById(R.id.tv1);//获取控件
tv1.setText("1233213");//用settext方法赋值
1 package com.example.aswork1; 2 3 import android.os.Bundle; 4 import android.util.Log; 5 import android.view.View; 6 import android.widget.Button; 7 import android.widget.EditText; 8 import android.widget.TextView; 9 10 import androidx.appcompat.app.AppCompatActivity; 11 12 public class MainActivity3 extends AppCompatActivity { 13 EditText et1; 14 EditText et2; 15 Button btn1; 16 Button btn2; 17 Button btn3; 18 Button btn4; 19 TextView tv1; 20 String str1; 21 String str2; 22 int a; 23 int b; 24 25 protected void onCreate(Bundle savedInstanceState) { 26 super.onCreate(savedInstanceState); 27 setContentView(R.layout.activity_main3); 28 et1=findViewById(R.id.et1); 29 et2=findViewById(R.id.et2); 30 btn1=findViewById(R.id.btn1); 31 btn2=findViewById(R.id.btn2); 32 btn3=findViewById(R.id.btn3); 33 btn4=findViewById(R.id.btn4); 34 tv1=findViewById(R.id.tv1); 35 btn1.setOnClickListener(new View.OnClickListener() { 36 @Override 37 public void onClick(View v) { 38 str1=et1.getText().toString(); 39 str2=et2.getText().toString(); 40 a=Integer.parseInt(str1); 41 b=Integer.parseInt(str2); 42 String str3=""+(a+b); 43 44 tv1.setText(str3); 45 } 46 }); 47 btn2.setOnClickListener(new View.OnClickListener() { 48 @Override 49 public void onClick(View v) { 50 str1=et1.getText().toString(); 51 str2=et2.getText().toString(); 52 a=Integer.parseInt(str1); 53 b=Integer.parseInt(str2); 54 String str3=""+(a-b); 55 56 tv1.setText(str3); 57 } 58 }); 59 btn3.setOnClickListener(new View.OnClickListener() { 60 @Override 61 public void onClick(View v) { 62 str1=et1.getText().toString(); 63 str2=et2.getText().toString(); 64 a=Integer.parseInt(str1); 65 b=Integer.parseInt(str2); 66 String str3=""+(a*b); 67 68 tv1.setText(str3); 69 } 70 }); 71 btn4.setOnClickListener(new View.OnClickListener() { 72 @Override 73 public void onClick(View v) { 74 str1=et1.getText().toString(); 75 str2=et2.getText().toString(); 76 a=Integer.parseInt(str1); 77 b=Integer.parseInt(str2); 78 String str3=""+(a/b); 79 80 tv1.setText(str3); 81 } 82 }); 83 } 84 }
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:id="@+id/rl_1" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 tools:context=".MainActivity3" 7 android:orientation="vertical"> 8 9 <EditText 10 android:layout_width="match_parent" 11 android:layout_height="50dp" 12 android:id="@+id/et1"/> 13 <EditText 14 android:layout_width="match_parent" 15 android:layout_height="50dp" 16 android:id="@+id/et2"/> 17 <Button 18 android:layout_width="wrap_content" 19 android:layout_height="wrap_content" 20 android:id="@+id/btn1" 21 android:text="加"/> 22 <Button 23 android:layout_width="wrap_content" 24 android:layout_height="wrap_content" 25 android:id="@+id/btn2" 26 android:text="减"/> 27 28 <Button 29 android:layout_width="wrap_content" 30 android:layout_height="wrap_content" 31 android:id="@+id/btn3" 32 android:text="乘"/> 33 <Button 34 android:layout_width="wrap_content" 35 android:layout_height="wrap_content" 36 android:id="@+id/btn4" 37 android:text="除"/> 38 <TextView 39 android:layout_width="match_parent" 40 android:layout_height="100dp" 41 android:id="@+id/tv1"/> 42 43 44 45 46 </LinearLayout>

 
                     
                    
                 
                    
                 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号