第四次Android作业

1.三个界面,界面1点击按钮使用显式意图开启界面2.
界面2点击按钮隐式意图开启界面3 

2.在界面1做一个按钮开启浏览器访问百度

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    >
<Button
    android:id="@+id/bt1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="启动界面二"
    />

<Button
    android:id="@+id/bt2"
    android:onClick="click2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="打开百度"
    android:layout_below="@id/bt1"
    />
</RelativeLayout>
<RelativeLayout 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=".SecondActivity">
    <Button
        android:id="@+id/bt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="启动界面三"
        />

</RelativeLayout>
<RelativeLayout 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=".ThirdActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello world!" />

</RelativeLayout>
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.content.Intent;
import android.view.View.OnClickListener;
import android.net.Uri;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button bt1=(Button)findViewById(R.id.bt1);
        Button bt2=(Button)findViewById(R.id.bt2);

        bt1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent=new Intent(MainActivity.this,SecondActivity.class);
                startActivity(intent);
            }
        });


    }
    public void click2(View view){
        Intent intent=new Intent();
        intent.setAction("android.intent.action.VIEW");
        intent.setData(Uri.parse("http://www.baidu.com"));
        startActivity(intent);
    }
}
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        Button bt2=(Button)findViewById(R.id.bt2);
        bt2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent=new Intent();
                intent.setAction("com.lrp.start");
                intent.addCategory("android.intent.category.DEFAULT");
                startActivity(intent);
            }
        });
    }
}
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class ThirdActivity extends AppCompatActivity {

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

 

 

 

 

3.2个edittext,4个按钮一个textview,实现简单计算器。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    >

    <EditText
        android:id="@+id/ed1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="50dp"
        android:layout_marginStart="10dp"
        android:layout_marginTop="20dp"
        android:ems="10"
        android:hint="第一个数"
        android:maxLines="2"
        android:textStyle="italic"
        android:layout_marginRight="50dp"
        android:layout_marginLeft="10dp">
    </EditText>

    <EditText
        android:id="@+id/ed2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="50dp"
        android:layout_marginStart="10dp"
        android:layout_marginTop="70dp"
        android:hint="第二个数"
        android:textStyle="italic"
        android:layout_marginRight="50dp"
        android:layout_marginLeft="10dp" />

    <Button
        android:id="@+id/btn_1"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/ed2"
        android:layout_marginTop="14dp"
        android:text="+"
        android:textSize="30dp"
        android:onClick="SUM"/>
    <Button
        android:id="@+id/btn_2"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_below="@+id/ed2"
        android:layout_marginTop="14dp"
        android:layout_marginLeft="100dp"
        android:text="-"
        android:textSize="30dp"
        android:onClick="SUB"/>
    <Button
        android:id="@+id/btn_3"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_below="@+id/ed2"
        android:layout_marginTop="14dp"
        android:layout_marginLeft="200dp"
        android:text="*"
        android:textSize="30dp"
        android:onClick="MUL"/>
    <Button
        android:id="@+id/btn_4"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_below="@+id/ed2"
        android:layout_marginTop="14dp"
        android:layout_marginLeft="300dp"
        android:text="/"
        android:textSize="30dp"
        android:onClick="DIV"/>
    <TextView
        android:id="@+id/tv1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="结果是:"
        android:textSize="30dp"
        android:layout_marginTop="220dp"></TextView>
</RelativeLayout>
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
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);
    }
    public void SUM(View v) {
        // TODO Auto-generated method stub
        EditText ed1=(EditText)findViewById(R.id.ed1);
        EditText ed2=(EditText)findViewById(R.id.ed2);
        TextView tv1=(TextView)findViewById(R.id.tv1);
        double num1=0;
        double num2=0;
        double ans=0;
        String numed1=ed1.getText().toString();
        String numed2=ed2.getText().toString();
        num1=Double.parseDouble(numed1);
        num2=Double.parseDouble(numed2);
        ans=num1+num2;
        tv1.setText(String.valueOf(ans));
    }
    public void SUB(View v) {
        // TODO Auto-generated method stub
        EditText ed1=(EditText)findViewById(R.id.ed1);
        EditText ed2=(EditText)findViewById(R.id.ed2);
        TextView tv1=(TextView)findViewById(R.id.tv1);
        double num1=0;
        double num2=0;
        double ans=0;
        String numed1=ed1.getText().toString();
        String numed2=ed2.getText().toString();
        num1=Double.parseDouble(numed1);
        num2=Double.parseDouble(numed2);
        ans=num1-num2;
        tv1.setText(String.valueOf(ans));
    }
    public void MUL(View v) {
        // TODO Auto-generated method stub
        EditText ed1=(EditText)findViewById(R.id.ed1);
        EditText ed2=(EditText)findViewById(R.id.ed2);
        TextView tv1=(TextView)findViewById(R.id.tv1);
        double num1=0;
        double num2=0;
        double ans=0;
        String numed1=ed1.getText().toString();
        String numed2=ed2.getText().toString();
        num1=Double.parseDouble(numed1);
        num2=Double.parseDouble(numed2);
        ans=num1*num2;
        tv1.setText(String.valueOf(ans));
    }
    public void DIV(View v) {
        // TODO Auto-generated method stub
        EditText ed1=(EditText)findViewById(R.id.ed1);
        EditText ed2=(EditText)findViewById(R.id.ed2);
        TextView tv1=(TextView)findViewById(R.id.tv1);
        double num1=0;
        double num2=0;
        double ans=0;
        String numed1=ed1.getText().toString();
        String numed2=ed2.getText().toString();
        num1=Double.parseDouble(numed1);
        num2=Double.parseDouble(numed2);
        ans=num1/num2;
        tv1.setText(String.valueOf(ans));
    }
}

 

 
posted @ 2021-10-03 17:52  星星邮寄者  阅读(35)  评论(0编辑  收藏  举报