第七周作业

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

package com.example.myapplication;
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);
            }
        });


    }

}
package com.example.myapplication;
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 SecondActivity extends Activity {

    @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.lg.start");
                intent.addCategory("android.intent.category.DEFAULT");
                startActivity(intent);
            }
        });
    }

}
package com.example.myapplication;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;


public class ThirdActivity extends Activity {

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



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

    <Button
        android:id="@+id/bt1"
        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"
>
    <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"
 >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello_world" />

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

    <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=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <activity android:name=".SecondActivity">

        </activity>

        <activity android:name=".ThirdActivity">
            <intent-filter>
                <action android:name="com.lg.start" />

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

        </activity>

    </application>

</manifest>

 

 

 

 

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: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.myapplication103;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
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);
        Log.e("MainActivity", "调用oncreate");
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        Log.e("MainActivity", "调用onstart");
    }
    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        Log.e("MainActivity", "调用onresume");
    }
    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        Log.e("MainActivity", "调用onpause");
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        Log.e("MainActivity", "调用onstop");
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Log.e("MainActivity", "调用ondestroy");
    }

    @Override
    protected void onRestart() {
        // TODO Auto-generated method stub
        super.onRestart();
        Log.e("MainActivity", "调用onRestart");
    }

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

 

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
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.b_1 ).setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String num1=((EditText)(findViewById( R.id.et_1 ))).getText().toString();//获取et_1上面的文本,并转成字符串
                String num2=((EditText)(findViewById( R.id.et_2 ))).getText().toString();//获取et_2上面的文本,并转成字符串
                int n1= Integer.parseInt( num1 );
                int n2=Integer.parseInt( num2 );
                int sum=n1+n2;
                TextView tv1=findViewById( R.id.tv2 );//获取TextView控件
                tv1.setText( "结果是"+sum );

            }
        } );
        findViewById( R.id.b_2 ).setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String num1=((EditText)(findViewById( R.id.et_1 ))).getText().toString();
                String num2=((EditText)(findViewById( R.id.et_2 ))).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.b_3 ).setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String num1=((EditText)(findViewById( R.id.et_1 ))).getText().toString();
                String num2=((EditText)(findViewById( R.id.et_2 ))).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.b_4 ).setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String num1=((EditText)(findViewById( R.id.et_1 ))).getText().toString();
                String num2=((EditText)(findViewById( R.id.et_2 ))).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.b_4).setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String num1=((EditText)(findViewById( R.id.et_1 ))).getText().toString();
                String num2=((EditText)(findViewById( R.id.et_2 ))).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);
            }
        } );
    }
}
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent">
    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="计算器"
        android:textSize="25dp"
        android:layout_margin="5dp"/>
    <EditText
        android:id="@+id/et_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入一个数"
        android:textSize="25dp"
        android:layout_marginTop="70dp"/>
    <EditText
        android:id="@+id/et_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入一个数"
        android:textSize="25dp"
        android:layout_below="@id/et_1"/>
    <Button
        android:id="@+id/b_1"
        android:layout_width="70dp"
        android:layout_height="50dp"
        android:text="+"
        android:textSize="25dp"
        android:background="#7C7C7C"
        android:layout_marginTop="200dp"
        android:layout_marginLeft="22dp"/>
    <Button
        android:id="@+id/b_2"
        android:layout_width="70dp"
        android:layout_height="50dp"
        android:text="-"
        android:textSize="25dp"
        android:background="#7C7C7C"
        android:layout_toRightOf="@id/b_1"
        android:layout_marginTop="200dp"
        android:layout_marginLeft="10dp"/>
    <Button
        android:id="@+id/b_3"
        android:layout_width="70dp"
        android:layout_height="50dp"
        android:text="*"
        android:textSize="25dp"
        android:background="#7C7C7C"
        android:layout_toRightOf="@id/b_2"
        android:layout_marginTop="200dp"
        android:layout_marginLeft="10dp"/>
    <Button
        android:id="@+id/b_4"
        android:layout_width="70dp"
        android:layout_height="50dp"
        android:text="/"
        android:textSize="25dp"
        android:background="#7C7C7C"
        android:layout_toRightOf="@id/b_3"
        android:layout_marginTop="200dp"
        android:layout_marginLeft="10dp"/>
    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25dp"
        android:text="结果"
        android:layout_marginTop="300dp"
        android:layout_marginLeft="10dp"/>

</RelativeLayout>

 

posted @ 2021-10-08 11:35  冯雨心  阅读(23)  评论(0编辑  收藏  举报