第七周作业

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

?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=".MainActivity">

    <Button
        android:id="@+id/bt_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#F2B4FC"
        android:text="显示意图开启界面2"
        android:textSize="20sp"
        android:textColor="#000000"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="10dp"/>

</LinearLayout>
package com.example.work;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
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,MainActivity.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);
    }
}
package com.example.work;

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

public class MainActivity extends Activity {

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

    public void click1(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.work;

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

public class MainActivity extends Activity {

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

    public void click1(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,实现简单计算器。

提示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方法赋值

<?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=".CalculatorActivity"
    android:layout_margin="20dp">
    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="数字:"
        android:textColor="#000000"
        android:textSize="20sp"
        android:layout_marginTop="15dp"/>
    <EditText
        android:id="@+id/et1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入数字"
        android:textSize="25sp"
        android:textColorHint="#DBA901"
        android:maxLines="1"
        android:scrollHorizontally="true"
        android:layout_toRightOf="@+id/tv1"/>
    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="数字:"
        android:textColor="#000000"
        android:textSize="20sp"
        android:layout_marginTop="25dp"
        android:layout_below="@id/tv1"/>
    <EditText
        android:id="@+id/et2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入数字"
        android:textSize="25sp"
        android:textColorHint="#DBA901"
        android:maxLines="1"
        android:scrollHorizontally="true"
        android:layout_toRightOf="@+id/tv1"
        android:layout_below="@id/et1"/>
    <Button
        android:id="@+id/btn_1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="+"
        android:textColor="#000000"
        android:textSize="50sp"
        android:layout_below="@id/et2"
        android:layout_marginTop="100dp"
        android:layout_margin="10dp"
        android:layout_alignLeft="@id/et2"
        android:background="@drawable/button2"/>
    <Button
        android:id="@+id/btn_2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="-"
        android:textColor="#000000"
        android:textSize="50sp"
        android:layout_below="@id/et2"
        android:layout_toRightOf="@id/btn_1"
        android:layout_alignTop="@id/btn_1"
        android:background="@drawable/button1"/>
    <Button
        android:id="@+id/btn_3"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_below="@id/btn_1"
        android:layout_margin="10dp"
        android:text="*"
        android:textColor="#000000"
        android:textSize="50sp"
        android:layout_alignLeft="@id/et2"
        android:background="@drawable/button2"/>
    <Button
        android:id="@+id/btn_4"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_below="@id/btn_1"
        android:layout_toRightOf="@id/btn_3"
        android:text="/"
        android:textColor="#000000"
        android:textSize="50sp"
        android:layout_alignTop="@id/btn_3"
        android:background="@drawable/button1"/>
    <TextView
        android:id="@+id/tv3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/btn_3"
        android:layout_margin="20dp"
        android:text="结果是:"
        android:textSize="45sp"
        android:textColor="#000000"
        android:textStyle="italic"/>
</RelativeLayout>
复制代码
复制代码
package com.example.myapplication3;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class CalculatorActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculator);
        //+
        findViewById(R.id.btn_1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String num1=((EditText)(findViewById(R.id.et1))).getText().toString();
                String num2=((EditText)(findViewById(R.id.et2))).getText().toString();
                int n1=Integer.parseInt(num1);
                int n2=Integer.parseInt(num2);
                int n3=n1+n2;
                TextView tv1=(TextView)findViewById(R.id.tv3);
                tv1.setText("结果是"+n3);
            }
        });
        //-
        findViewById(R.id.btn_2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String num1=((EditText)(findViewById(R.id.et1))).getText().toString();
                String num2=((EditText)(findViewById(R.id.et2))).getText().toString();
                int n1=Integer.parseInt(num1);
                int n2=Integer.parseInt(num2);
                int n3=n1-n2;
                TextView tv1=(TextView)findViewById(R.id.tv3);
                tv1.setText("结果是"+n3);
            }
        });
        //*
        findViewById(R.id.btn_3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String num1=((EditText)(findViewById(R.id.et1))).getText().toString();
                String num2=((EditText)(findViewById(R.id.et2))).getText().toString();
                int n1=Integer.parseInt(num1);
                int n2=Integer.parseInt(num2);
                int n3=n1*n2;
                TextView tv1=(TextView)findViewById(R.id.tv3);
                tv1.setText("结果是"+n3);
            }
        });
        //
        findViewById(R.id.btn_4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String num1=((EditText)(findViewById(R.id.et1))).getText().toString();
                String num2=((EditText)(findViewById(R.id.et2))).getText().toString();
                int n1=Integer.parseInt(num1);
                int n2=Integer.parseInt(num2);
                int n3=n1/n2;
                TextView tv1=(TextView)findViewById(R.id.tv3);
                tv1.setText("结果是"+n3);
            }
        });
    }

 

posted on 2021-10-08 21:16  庞博  阅读(10)  评论(0编辑  收藏  举报

导航