第七周
1.三个界面,界面1点击按钮使用显式意图开启界面2.
界面2点击按钮隐式意图开启界面3
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.content.Intent; import android.view.View; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);}public void click1(View view){ Intent intent=new Intent(this,oneActivity.class); startActivity(intent); } public void click2(View view){ Intent intent=new Intent(); intent.setAction("cn.it.ab_cd"); intent.addCategory("android.intent.category.DEFAULT"); startActivity(intent); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/v1" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="click1" android:text="按钮1开启界面2" android:textColor="#FFC0CB" android:textSize="30sp" /> <Button android:id="@+id/v2" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="click2" android:text="按钮2开启界面3" android:textSize="30sp" android:textColor="#DDA0DD" /> </LinearLayout>
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; public class oneActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_one); } }
<?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=".oneActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="界面2" android:textSize="40dp" android:textColor="#87CEFA" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> </RelativeLayout>
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; public class twoActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_two); } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myapplication"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".twoActivity"> <intent-filter> <action android:name="cn.it.ab_cd"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </activity> <activity android:name=".oneActivity" /> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
<?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=".twoActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="界面3" android:textSize="40dp" android:textColor="#9370DB" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> </RelativeLayout>
2.在界面1做一个按钮开启浏览器访问百度
package com.example.myapplication; 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 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); } }); } }
<?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"> <Button android:id="@+id/bt_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#3F51B5" android:text="显式意图开启界面2" android:textSize="20sp" android:layout_marginTop="20dp" android:layout_marginLeft="20dp"/> <Button android:id="@+id/bt_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#4CAF50" android:text="开启浏览器访问百度" android:textSize="20sp" android:layout_marginTop="20dp" android:layout_marginLeft="20dp" android:padding="10dp" android:onClick="click1"/> </LinearLayout>
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.myapplication; 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); } } ); } }