其他分享
首页 > 其他分享> > 安卓第7周

安卓第7周

作者:互联网

1.

<?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/B_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"/>
</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=".Main2Activity">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       android:textSize="20dp"
        android:text="界面2"/>
<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:text="界面3"/>
</LinearLayout>
package com.example.h1;

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.h2;

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

<?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"
    android:orientation="vertical">

    <Button
        android:id="@+id/bt_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="开启访问百度"
        android:textSize="30dp"
        android:onClick="click1" />

</LinearLayout>

 package="com.example.myapplication3">

    <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=".ThirdActivity">
            <intent-filter>
                <action android:name="com.example.start" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity android:name=".SecondActivity" >

        </activity>
        <activity android:name=".IntentActivity" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        </activity>
        <activity android:name=".ButtonActivity">

        </activity>
        <activity android:name=".MainActivity" />
    </application>

</manifest>

 

 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=".Main2Activity"
    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="#E91313"
        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="#E91313"
        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" />
    <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" />
    <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" />
    <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" />
    <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.myapplication;

import androidx.appcompat.app.AppCompatActivity;

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

public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        //+
        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);
            }
        });
    }
}

 

标签:findViewById,int,安卓,import,android,id,View
来源: https://www.cnblogs.com/hyp33/p/15377496.html