Android程序第4次作业

package com.example.myapplication;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.content.Intent;
import android.widget.Button;

public class MainActivity04 extends Activity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button)findViewById(R.id.buttonNextMainActivity);
        button.setOnClickListener(this);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
//        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    @Override
    public void onClick(View v) {
    	Intent intent = new Intent(this, SecondActivity04.class);
		startActivity(intent);
    }
}

<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity04" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="activity_main" />
    <Button
        android:id="@+id/buttonNextMainActivity"
        android:layout_width="wrap_content"
		android:layout_height="wrap_content"
        android:text="next" />

</LinearLayout>

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.widget.Button;

public class SecondActivity04 extends Activity implements View.OnClickListener  {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second04);
        Button button = (Button)findViewById(R.id.buttonNextSecondActivity);
        button.setOnClickListener(this);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
//        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    @Override
    public void onClick(View v) {
    	if (v.getId() == R.id.buttonNextSecondActivity) {
    		Intent intent = new Intent();
    		intent.setAction("com.example.assignment04.THIRD_ACTIVITY");
    		startActivity(intent);
    	}
    }
}

<LinearLayout 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"
	android:orientation="vertical"
    tools:context=".SecondActivity04" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="activity_second" />
    <Button
        android:id="@+id/buttonNextSecondActivity"
        android:layout_width="wrap_content"
		android:layout_height="wrap_content"
        android:text="next" />
</LinearLayout>
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);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
//        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".ThirdActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="activity_third" />

</LinearLayout>

package com.example.myapplication;

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

public class Assignment04_01 extends Activity {

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

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
//		getMenuInflater().inflate(R.menu.assignment02, menu);
		return true;
	}
	
	public void clicked(View v) {
		Intent intent = new Intent();
		intent.setAction("android.intent.action.VIEW");
		intent.setData(Uri.parse("http://www.baidu.com"));
		startActivity(intent);
	}

}

<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".Assignment04_01" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="assignment02" />
    
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="access baidu"
        android:onClick="clicked" />

</LinearLayout>


package com.example.myapplication;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


public class Assignment04_03 extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_assignment04_03);
		Button addButton = findViewById(R.id.buttonAdd);
		addButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				EditText editResult = findViewById(R.id.textResult);
				EditText editFirst = findViewById(R.id.editTextFirstNumber);
				EditText editSecond = findViewById(R.id.editTextSecondNumber);

				editResult.setText(String.valueOf(add(editFirst.getText().toString(), editSecond.getText().toString())));
			}
		});
		Button subButton = findViewById(R.id.buttonSub);
		subButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				EditText editResult = findViewById(R.id.textResult);
				EditText editFirst = findViewById(R.id.editTextFirstNumber);
				EditText editSecond = findViewById(R.id.editTextSecondNumber);
				editResult.setText(String.valueOf(sub(editFirst.getText().toString(), editSecond.getText().toString())));
			}
		});
		Button mulButton = findViewById(R.id.buttonMul);
		mulButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				EditText editResult = findViewById(R.id.textResult);
				EditText editFirst = findViewById(R.id.editTextFirstNumber);
				EditText editSecond = findViewById(R.id.editTextSecondNumber);

				editResult.setText(String.valueOf(mul(editFirst.getText().toString(), editSecond.getText().toString())));
			}
		});
		Button divButton = findViewById(R.id.buttonDiv);
		divButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				EditText editResult = findViewById(R.id.textResult);
				EditText editFirst = findViewById(R.id.editTextFirstNumber);
				EditText editSecond = findViewById(R.id.editTextSecondNumber);

				editResult.setText(String.valueOf(div(editFirst.getText().toString(), editSecond.getText().toString())));
			}
		});
	}


	private int add(String left, String right) {
		int _left = Integer.parseInt(left);
		int _right = Integer.parseInt(right);



		return _left + _right;
	}

	private int sub(String left, String right) {
		int _left = Integer.parseInt(left);
		int _right = Integer.parseInt(right);



		return _left - _right;
	}

	private int mul(String left, String right) {
		int _left = Integer.parseInt(left);
		int _right = Integer.parseInt(right);



		return _left * _right;
	}

	private double div(String left, String right) {
		int _left = Integer.parseInt(left);

		int _right = Integer.parseInt(right);
		return (double)_left / _right;
	}
}











<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".Assignment04_03" >
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    
		<TextView
		  	android:id="@+id/textViewFirstNumber"
		    android:layout_width="wrap_content"
		    android:layout_height="wrap_content"
		    android:text="first number" />
		<EditText
            android:id="@+id/editTextFirstNumber"
		    android:layout_width="160dp"
		    android:layout_height="61dp"
		    android:layout_toRightOf="@id/textViewFirstNumber"
		    android:hint="first number:" />
	</LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    
		<TextView
		    android:id="@+id/textViewSecondNumber"
		    android:layout_width="wrap_content"
		    android:layout_height="wrap_content"
		    android:text="second number:" />
		
		<EditText
            android:id="@+id/editTextSecondNumber"

		    android:layout_width="160dp"
		    android:layout_height="61dp"
		    android:layout_toRightOf="@id/textViewSecondNumber"
		    android:hint="second number" />
	
	</LinearLayout>
	
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="160dp">
		
        <Button 
            android:id="@+id/buttonAdd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="+" />
        
        <Button 
            android:id="@+id/buttonSub"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="-"
            android:layout_toRightOf="@id/buttonAdd" />
        
		<Button 
            android:id="@+id/buttonMul"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="*"
            android:layout_toRightOf="@id/buttonSub" />
		
		<Button 
            android:id="@+id/buttonDiv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="/"
            android:layout_toRightOf="@id/buttonMul" />
        
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="wrap_content"
		android:layout_height="wrap_content">
		<TextView 
		    android:id="@+id/__result"
		    android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="result: " />
		<EditText
		    android:id="@+id/textResult"
		    android:layout_width="wrap_content"
		    android:layout_height="wrap_content" 
		    android:hint="result" 
		    android:layout_toRightOf="@id/__result" />
    </LinearLayout>

</LinearLayout>

posted @ 2021-10-09 16:11  SuoJing  阅读(38)  评论(0编辑  收藏  举报