安卓第四次作业

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:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="显式意图开启界面2" />

    <Button
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="开启浏览器访问百度"
        android:onClick="click3" />

</LinearLayout>
package com.example.week7;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn1=(Button)findViewById(R.id.btn1);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(MainActivity.this,Main2Activity.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,实现简单计算器

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/xb_1"
    tools:context=".Main4Activity">

    <EditText
        android:id="@+id/et_1"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:layout_marginLeft="100dp"
        android:layout_marginRight="100dp"
        android:inputType="number"
        android:gravity="center"
        android:textSize="20sp"/>

    <EditText
        android:id="@+id/et_2"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="100dp"
        android:layout_marginRight="100dp"
        android:inputType="number"
        android:gravity="center"
        android:textSize="20sp"/>
    <LinearLayout
        android:id="@+id/xb_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:orientation="horizontal">
        <Button
            android:id="@+id/btn_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="加(+)"/>
        <Button
            android:id="@+id/btn_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="减(-)"/>
        <Button
            android:id="@+id/btn_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="乘(*)"/>
         <Button
             android:id="@+id/btn_4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="除(/)"/>
    </LinearLayout>
    <TextView
        android:id="@+id/tv_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:layout_marginLeft="150dp"
        android:layout_marginRight="150dp"
        android:textSize="30sp"
        android:gravity="center"
        android:textColor="#06C6F7"
        android:text="结果为"/>

</LinearLayout>
package com.example.week7;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Main4Activity extends AppCompatActivity {
    private TextView tv_1;
    private Button btn_1,btn_2,btn_3,btn_4;
    private EditText et_1,et_2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main4);
        btn_1=(Button)this.findViewById(R.id.btn_1);
        btn_2=(Button)findViewById(R.id.btn_2);
        btn_3=(Button)findViewById(R.id.btn_3);
        btn_4=(Button)findViewById(R.id.btn_4);
        et_1=(EditText)this.findViewById(R.id.et_1);
        et_2=(EditText)this.findViewById(R.id.et_2);
        tv_1=(TextView)this.findViewById(R.id.tv_1);

        btn_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String inputText1=et_1.getText().toString();
                String inputText2=et_2.getText().toString();
                float num1=Float.valueOf(inputText1).intValue();
                float num2=Float.valueOf(inputText2).intValue();
                num1=num1+num2;
                inputText1=String.valueOf(num1);
                tv_1.setText(inputText1);
            }
        });
        btn_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String inputText1=et_1.getText().toString();
                String inputText2=et_2.getText().toString();
                float num1=Float.valueOf(inputText1).intValue();
                float num2=Float.valueOf(inputText2).intValue();
                num1=num1-num2;
                inputText1=String.valueOf(num1);
                tv_1.setText(inputText1);
            }
        });
        btn_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String inputText1=et_1.getText().toString();
                String inputText2=et_2.getText().toString();
                float num1=Float.valueOf(inputText1).intValue();
                float num2=Float.valueOf(inputText2).intValue();
                num1=num1*num2;
                inputText1=String.valueOf(num1);
                tv_1.setText(inputText1);
            }
        });
        btn_4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String inputText1=et_1.getText().toString();
                String inputText2=et_2.getText().toString();
                float num1=Float.valueOf(inputText1).intValue();
                float num2=Float.valueOf(inputText2).intValue();
                if (num2==0){
                    Toast.makeText(Main4Activity.this,"除数不能为0",Toast.LENGTH_LONG).show();
                }else{
                    num1=num1/num2;
                    inputText1=String.valueOf(num1);
                    tv_1.setText(inputText1);
                }
            }
        });
    }
}

posted @ 2021-10-08 22:33  MaLD  阅读(11)  评论(0编辑  收藏  举报