第七周作业

1.三个界面,界面1点击按钮使用显式意图开启界面2.
界面2点击按钮隐式意图开启界面3
2.在界面1做一个按钮开启浏览器访问百度

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

    <Button
        android:id="@+id/bt1"
        android:onClick="onclick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="显示意图开启界面2"

        />
    <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"
        />



</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=".secondActivity"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="界面2"
        android:textSize="36dp"
        />
    <Button
        android:id="@+id/bt3"
        android:onClick="click3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="隐式意图开启界面3"
        android:layout_below="@id/bt2"
        />


</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=".secondActivity"
    android:gravity="center">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="界面3"
        android:textSize="36dp"
        />

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.anint">

    <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="cn.itcast.START_ACTIVITY" />

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

        </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>

package com.example.anint;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

@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.anint;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class secondActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
public void click3(View v){
Intent intent = new Intent();
intent.setAction("cn.itcast.START_ACTIVITY");
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: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">
    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="计算器"
        android:textSize="36dp"
        android:layout_margin="30dp"
        />
    <EditText
        android:id="@+id/et1"
        android:layout_width="360dp"
        android:layout_height="60dp"/>
    <EditText
        android:id="@+id/et2"
        android:layout_width="360dp"
        android:layout_height="60dp"/>


    <Button
        android:id="@+id/btn_1"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:text="+"
        android:textSize="50dp"/>
    <Button
        android:id="@+id/btn_2"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:text="-"
    android:textSize="50dp"/>
    <Button
        android:id="@+id/btn_3"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:text="*"
    android:textSize="60dp"/>
    <Button
        android:id="@+id/btn_4"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:text="/"
    android:textSize="60dp"/>
    <TextView
        android:id="@+id/tv2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="结果是"
        android:textSize="36dp"
        android:layout_margin="26dp"/>
</LinearLayout>
package com.example.jisuan;

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);
        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 sum=n1+n2;
                TextView tv1=findViewById( R.id.tv2 );
                tv1.setText( "结果是"+sum );
            }
        } );
        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 sum=n1-n2;
                TextView tv1=findViewById( R.id.tv2 );
                tv1.setText("结果是"+sum);
            }
        } );
        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 sum=n1*n2;
                TextView tv1=findViewById( R.id.tv2 );
                tv1.setText("结果是"+sum);
            }
        } );


        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 sum=n1/n2;
                TextView tv1=findViewById( R.id.tv2 );
                tv1.setText("结果是"+sum);
            }
        } );
    }
}

 

posted @ 2021-10-08 18:40  崔明月  阅读(29)  评论(0编辑  收藏  举报