Android第7周作业
1.三个界面,界面1点击按钮使用显式意图开启界面2.
界面2点击按钮隐式意图开启界面3
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?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:layout_margin="20dp" android:orientation="vertical"> <Button android:id="@+id/interface2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="显示意图 --> 界面二" /> <Button android:id="@+id/interface3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="隐式意图 --> 界面三" /> </LinearLayout>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?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" android:orientation="vertical" tools:context=".Second_Activity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="30dp" android:text="Second_interface" android:textSize="40sp" android:textStyle="bold" /> <Button android:id="@+id/interface2_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="隐式意图 --> 界面三" /> </LinearLayout>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?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=".Third_Activity" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Third_interface" android:textSize="40sp" android:textStyle="bold" android:layout_margin="30dp"/> </LinearLayout>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Object Second_Activity; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.interface2).setOnClickListener(this); findViewById(R.id.interface3).setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.interface2: Intent intent1 = new Intent(MainActivity.this,Second_Activity.class); startActivity(intent1); break; case R.id.interface3: Intent intent2 = new Intent(); intent2.setAction("com.example.START_ACTIVITY"); startActivity(intent2); break; } } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; public class Second_Activity extends AppCompatActivity implements View.OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second_); findViewById(R.id.interface2_3).setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.interface2_3: Intent intent2_3 = new Intent(); intent2_3.setAction("com.example.START_ACTIVITY"); startActivity(intent2_3); break; } } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?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=".Third_Activity"> <intent-filter> <action android:name="com.example.START_ACTIVITY" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> <activity android:name=".Second_Activity" /> <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>
2.在界面1做一个按钮开启浏览器访问百度
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?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:layout_margin="20dp" android:orientation="vertical"> <Button android:id="@+id/interface2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="显示意图 --> 界面二" /> <Button android:id="@+id/interface3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="隐式意图 --> 界面三" /> <Button android:id="@+id/OpenBrowser" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="OpenBrowser --> baidu.com"/> </LinearLayout>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Object Second_Activity; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.interface2).setOnClickListener(this); findViewById(R.id.interface3).setOnClickListener(this); findViewById(R.id.OpenBrowser).setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.interface2: Intent intent1 = new Intent(MainActivity.this,Second_Activity.class); startActivity(intent1); break; case R.id.interface3: Intent intent2 = new Intent(); intent2.setAction("com.example.START_ACTIVITY"); startActivity(intent2); break; case R.id.OpenBrowser: Intent intent3 = new Intent(); // 设置开启系统浏览器动作 intent3.setAction("android.intent.action.VIEW"); // 设置要打开的网页 intent3.setData(Uri.parse("http://baidu.com")); startActivity(intent3); break; } } }
3.2个edittext,4个按钮一个textview,实现简单计算器。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?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:layout_margin="20dp" android:orientation="vertical"> <EditText android:id="@+id/et1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="10dp" android:gravity="center" android:maxLines="1" android:textSize="50sp" android:phoneNumber="true" android:scrollHorizontally="false" /> <TextView android:id="@+id/tv_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textColor="#FF0000" android:textSize="30sp" android:text="运算符"/> <EditText android:id="@+id/et2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="10dp" android:gravity="center" android:maxLines="1" android:textSize="50sp" android:phoneNumber="true" android:scrollHorizontally="false" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/sum" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="加(+)" /> <Button android:id="@+id/minus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="减(-)" /> <Button android:id="@+id/product" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="乘(*)" /> <Button android:id="@+id/divide" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="除(/)" /> </LinearLayout> <TextView android:id="@+id/tv_2" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textSize="60sp" android:textColor="#0000FF" /> </LinearLayout>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Object Second_Activity; private EditText et1,et2; private TextView tv1,tv2; private String textx; private float num; private Button sum,minus,product,divide; private Toast toast; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et1 = (EditText)findViewById(R.id.et1); tv1 = findViewById(R.id.tv_1); tv2 = findViewById(R.id.tv_2); sum = findViewById(R.id.sum); sum.setOnClickListener(this); minus = findViewById(R.id.minus); minus.setOnClickListener(this); product = findViewById(R.id.product); product.setOnClickListener(this); divide = findViewById(R.id.divide); divide.setOnClickListener(this); } @Override public void onClick(View view) { et2 = (EditText)findViewById(R.id.et2); float num1 = Integer.parseInt(et1.getText().toString()); float num2 = Integer.parseInt(et2.getText().toString()); switch (view.getId()){ case R.id.sum: num = num1 +num2; tv1.setText("加(+)"); break; case R.id.minus: num = num1 -num2; tv1.setText("减(-)"); break; case R.id.product: num = num1 *num2; tv1.setText("乘(*)"); break; case R.id.divide: num = (float)(num1*1.0 /num2); tv1.setText("除(/)"); if (num2 == 0){ Toast.makeText(this,"错误",Toast.LENGTH_LONG).show(); } break; } textx = String.valueOf(num); tv2.setText(textx); } }