Mobile development technology (Android) -- Experiment 3 common basic Android controls

1, Experimental purpose

  1. Master the syntax and format of adding controls to XML files;
  2. Master the common properties of controls;
  3. Master the method of obtaining controls in Java code;
  4. Familiar with the common properties and methods of text box, edit box, ordinary button and picture button.
  5. Master several ways to implement Button and ImageButton click events.
  6. Familiar with Toast.

2, Experimental content

1. Information registration

Create an Android project named "shiyan0301"_ Information registration_ Student name ", requirements:

  • Interface composition: seven text boxes are used to display student number, name, gender, date of birth, professional class, e-mail and registration information; 6 edit boxes for inputting student number, name, gender, date of birth, professional class and e-mail; Two common buttons "register" and "reset".
  • Program functions:
    ① When clicking the "registration" button, the information entered by the user will be displayed in a registration information text box;
    ② When you click the reset button, the information entered in the edit box is cleared.
layout xml code.......................................

<GridLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" 
    android:columnCount="3">

    <TextView
        android:id="@+id/num_TV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Student number" />
    <EditText 
        android:id="@+id/num_ET"
        android:layout_columnSpan="2"
        android:layout_gravity="fill_horizontal"
        android:layout_height="wrap_content"/>
    
    <TextView
        android:id="@+id/name_TV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="full name" />
    <EditText 
        android:id="@+id/name_ET"
        android:layout_columnSpan="2"
        android:layout_gravity="fill_horizontal"
        android:layout_height="wrap_content"/>
    
    <TextView
        android:id="@+id/sex_TV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Gender" />
    <EditText 
        android:id="@+id/sex_EV"
        android:layout_columnSpan="2"
        android:layout_gravity="fill_horizontal"
        android:layout_height="wrap_content"/>
    
    <TextView
        android:id="@+id/birthday_TV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="date of birth" />
    <EditText 
        android:id="@+id/birthday_EV"
        android:layout_columnSpan="2"
        android:layout_gravity="fill_horizontal"
        android:layout_height="wrap_content"/>
    
    <TextView
        android:id="@+id/class_TV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Professional class" />
    <EditText 
        android:id="@+id/class_EV"
       android:layout_columnSpan="2"
        android:layout_gravity="fill_horizontal"
        android:layout_height="wrap_content"/>
    
    <TextView
        android:id="@+id/mail_TV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="E-mail" />
    <EditText 
        android:id="@+id/mail_EV"
        android:layout_columnSpan="2"
        android:autoLink="email"
        android:layout_gravity="fill_horizontal"
        android:layout_height="wrap_content"/>
    
    <TextView
        android:id="@+id/information_TV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Registration information" />
    <EditText 
        android:id="@+id/information_EV"
        android:layout_columnSpan="2"
        android:layout_gravity="fill_horizontal"
        android:layout_height="wrap_content"/>
    <Button 
        android:id="@+id/register_BT"
        android:text="register"
        android:layout_width="wrap_content"/>
    <Button 
        android:id="@+id/request_BT"
        android:text="Reset"
        android:layout_width="wrap_content"/>
</GridLayout>
JAVA code.......................................

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

public class MainActivity extends Activity {

	EditText num,name,sex,birthday,department,mail,information;
	Button register,request;
	String String_num,String_name,String_sex,String_birthday,String_department,String_mail,String_information;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		num=(EditText) findViewById(R.id.num_ET);
		name=(EditText) findViewById(R.id.name_ET);
		sex=(EditText) findViewById(R.id.sex_EV);
		birthday=(EditText) findViewById(R.id.birthday_EV);
		department=(EditText) findViewById(R.id.class_EV);
		mail=(EditText) findViewById(R.id.mail_EV);
		information=(EditText) findViewById(R.id.information_EV);

		register=(Button) findViewById(R.id.register_BT);
		request=(Button) findViewById(R.id.request_BT);

		register.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String_num=num.getText().toString();
				String_name=name.getText().toString();
				String_sex=sex.getText().toString();
				String_birthday=birthday.getText().toString();
				String_department=department.getText().toString();
				String_mail=mail.getText().toString();

				information.setText(String_num+" "+String_name+" "+String_sex+" "+String_birthday+" "+String_department+" "+String_mail);
			}
		});


		request.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				num.setText("");
				name.setText("");
				sex.setText("");
				birthday.setText("");
				department.setText("");
				mail.setText("");
				information.setText("");
			}
		});
	}

	@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;
	}
}

2. User login

Create an Android project named "shiyan0302"_ User login_ Student name ", requirements:

  • Interface composition: two text boxes are used to display "user name:", "password:"; Two edit boxes are used to input user name and password information respectively. It is required that the password cannot be displayed in clear text; 1 ordinary button "login".
  • Program function: when clicking the "login" button, the following functions shall be realized:
    ① Judge that the user name and password are not empty. If they are empty, Toast prompt will be given;
    ② If the user name is the student's own name and the password is the student's student number, the Toast prompt message "login succeeded!"
    ③ If the user name is not the student's own name, or the password is not the student's own student number, the Toast prompt message "wrong user name or password" will be given, and the edit box will be cleared.
    ④ If the number of input errors exceeds 3 times, exit the program( Prompt, use the finish() method)
layout xml code.......................................

<TableLayout 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:layout_gravity="fill"
    android:columnCount="2"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:stretchColumns="1"
    tools:context=".MainActivity" >

    <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="user name" />
        <EditText
            android:id="@+id/userName_ET"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </TableRow>

    <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="dense    code" />
        <EditText
            android:id="@+id/passWord_ET"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword" />
    </TableRow>

    <Button
        android:id="@+id/login_BT"
        android:text="Sign in" />

    <TextView
        android:text="User name: 180000, password: 0302"
        android:textSize="20sp" />

</TableLayout>
JAVA code.......................................

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

public class MainActivity extends Activity {

	EditText userName,passWord;
	Button login;
	String userName_St,passWord_St;
	int num=0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		userName=(EditText) findViewById(R.id.userName_ET);
		passWord=(EditText) findViewById(R.id.passWord_ET);
		login=(Button) findViewById(R.id.login_BT);

		login.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				num++;
				userName_St=userName.getText().toString();
				passWord_St=passWord.getText().toString();

				if(userName_St.isEmpty()||passWord_St.isEmpty()){
					Toast toast = Toast.makeText(MainActivity.this, "User name or password cannot be empty", Toast.LENGTH_SHORT);
					toast.show();
					if(num>3){
						num=0;
						finish();
					}
				}else if(userName_St.equals("187427")&&passWord_St.equals("0302")){
					Toast toast = Toast.makeText(MainActivity.this, "Login succeeded", Toast.LENGTH_SHORT);
					toast.show();
					num=0;

				}else{
					Toast toast = Toast.makeText(MainActivity.this, "Wrong user name or password", Toast.LENGTH_SHORT);
					toast.show();
					if(num>3){
						num=0;
						finish();
					}
				}
			}
		});

	}

	@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;
	}
}

3. Simple calculator

Create an Android project named "shiyan0303"_ Simple calculator_ Student name ", requirements:

  • Interface composition: three text boxes are used to display "operation number one", "operation number two" and "calculation result" respectively; Two edit boxes are used to input two operands respectively; 4 ordinary buttons "add", "subtract", "multiply" and "divide".
  • Program function: when you click the "add", "subtract", "multiply" and "divide" buttons, the corresponding mathematical operations of the two operands are realized, and the results are displayed in the "calculation result" text box.
layout xml code.......................................

<GridLayout 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:columnCount="2"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/num1_TV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Operation number one" />

    <EditText
        android:id="@+id/num1_ET"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/num2_TV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Arithmetic number two" />

    <EditText
        android:id="@+id/num2_ET"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/outcome_TV"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_columnSpan="2"
        android:gravity="center_horizontal"
        android:text="Calculation results"
        android:textSize="25dp" />

    <Button
        android:id="@+id/add_BT"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="plus" />

    <Button
        android:id="@+id/sub_BT"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="reduce" />

    <Button
        android:id="@+id/mul_BT"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ride" />

    <Button
        android:id="@+id/div_BT"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="except" />

</GridLayout>
JAVA code.......................................

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

public class MainActivity extends Activity {

	TextView outcome;
	EditText num1,num2;
	Button add,sub,mul,div;
	double d1,d2;
	String num11,num22;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		num1=(EditText) findViewById(R.id.num1_ET);
		num2=(EditText) findViewById(R.id.num2_ET);
		outcome=(TextView) findViewById(R.id.outcome_TV);
		add=(Button) findViewById(R.id.add_BT);
		sub=(Button) findViewById(R.id.sub_BT);
		mul=(Button) findViewById(R.id.mul_BT);
		div=(Button) findViewById(R.id.div_BT);

		add.setOnClickListener(new OnListener());
		sub.setOnClickListener(new OnListener());
		mul.setOnClickListener(new OnListener());
		div.setOnClickListener(new OnListener());

	}


	@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;
	}


	class OnListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub

			num11=num1.getText().toString();

			d1 = Double.parseDouble(num11);
			num22=num2.getText().toString();
			d2 = Double.parseDouble(num22);

			if(v.getId()==R.id.add_BT){
				double all=d1+d2;
				outcome.setText(all+"");
			}else if(v.getId()==R.id.sub_BT){
				double all=d1-d2;
				outcome.setText(all+"");
			}else if(v.getId()==R.id.mul_BT){
				double all=d1*d2;
				outcome.setText(all+"");
			}else if(v.getId()==R.id.div_BT){
				double all=d1/d2;
				outcome.setText(all+"");
			}else{

			}
		}
	}
}

4. Triangle

Create an Android project named "shiyan0304"_ Triangle_ Student name ", requirements:

  • Interface composition: three text boxes are used to display "edge A", "edge B" and "edge C" respectively; Three edit boxes are used to input the three sides of the triangle respectively; 3 ordinary buttons "calculate perimeter", "calculate area" and "reset".
  • Program functions:
    ① When you click the calculate perimeter button, the perimeter of the triangle is displayed in the Toast prompt;
    ② When you click the calculate area button, the perimeter of the triangle is displayed in the Toast prompt;
    ③ Click the reset button to clear the information entered in the edit box.
layout xml code.......................................

<GridLayout 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:columnCount="3"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center_horizontal"
        android:text="edge A" />

    <EditText
        android:id="@+id/A"
        android:layout_columnSpan="2"
        android:layout_gravity="fill_horizontal" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center_horizontal"
        android:text="edge B" />

    <EditText
        android:id="@+id/B"
        android:layout_columnSpan="2"
        android:layout_gravity="fill_horizontal" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center_horizontal"
        android:text="edge C" />

    <EditText
        android:id="@+id/C"
        android:layout_columnSpan="2"
        android:layout_gravity="fill_horizontal" />

    <Button
        android:id="@+id/reset"
        android:layout_width="wrap_content"
        android:onClick="resetClick"
        android:text="Reset" />

    <Button
        android:id="@+id/circumference"
        android:layout_width="wrap_content"
        android:onClick="circumferenceClick"
        android:text="Calculated perimeter" />

    <Button
        android:id="@+id/area"
        android:layout_width="wrap_content"
        android:onClick="areaClick"
        android:text="Calculated area" />

</GridLayout>
JAVA code.......................................

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

public class MainActivity extends Activity {

	EditText A,B,C;
	Button reset,circumference,area;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		A=(EditText) findViewById(R.id.A);
		B=(EditText) findViewById(R.id.B);
		C=(EditText) findViewById(R.id.C);
		reset=(Button) findViewById(R.id.reset);
		circumference=(Button) findViewById(R.id.circumference);
		area=(Button) findViewById(R.id.area);
	}

	public void resetClick(View view){
		A.setText("");
		B.setText("");
		C.setText("");
		Toast toast = Toast.makeText(MainActivity.this,"Cleared", Toast.LENGTH_SHORT);
		toast.show();
	}

	public void circumferenceClick(View view){
		String sideA=A.getText().toString();
		String sideB=B.getText().toString();
		String sideC=C.getText().toString();

		double sA=Double.parseDouble(sideA);
		double sB=Double.parseDouble(sideB);
		double sC=Double.parseDouble(sideC);



		Toast toast = Toast.makeText(MainActivity.this,sA+sB+sC+"", Toast.LENGTH_SHORT);
		toast.show();
	}

	public void areaClick(View view){
		String sideA=A.getText().toString();
		String sideB=B.getText().toString();
		String sideC=C.getText().toString();

		double sA=Double.parseDouble(sideA);
		double sB=Double.parseDouble(sideB);
		double sC=Double.parseDouble(sideC);

		double p=(sA+sB+sC)/2;
		double s=Math.sqrt(p*(p-sA)*(p-sB)*(p-sC));

		Toast toast = Toast.makeText(MainActivity.this,s+"", Toast.LENGTH_SHORT);
		toast.show();
	}

	@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;
	}
}

Tags: Java Android SQLite

Posted on Fri, 03 Sep 2021 17:50:42 -0400 by tonypr100