实验 3 Android常用基本控件(一)

实验 3 Android常用基本控件(一)

    • 1.信息登记
            • string.xml
            • activity_main.xml
            • MainActivity.java
    • 3.简易计算器
            • string.xml
            • activity_main.xml
            • MainActivity.java
  • 总结


1.信息登记

string.xml
<string name="titile">学生信息登记string>
    <string name="stu_no">学号:string>
    <string name="stu_name">姓名:string>
    <string name="stu_sex">性别:string>
    <string name="stu_birthday">出生年月:string>
    <string name="stu_class">专业班级:string>
    <string name="stu_email">电子邮箱:string>
    <string name="stu_result">登记信息:string>
    
    <string name="hint_no">请输入学号string>
    <string name="hint_name">请输入姓名string>
    <string name="hint_sex">请输入性别string>
    <string name="hint_birthday">请输入出生年月string>
    <string name="hint_class">请输入专业班级string>
    <string name="hint_email">请输入电子邮箱string>
    
    <string name="btn_enregister">登记string>
    <string name="btn_resetting">重置string>
activity_main.xml
<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: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:orientation="vertical"
    android:gravity="center_horizontal" 
    android:padding="10dp">
    
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_gravity="center"
        android:textSize="18sp"
        android:textColor="#f00"
        android:text="@string/titile"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/stu_no"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="@string/stu_no" />

        <EditText
            android:id="@+id/edit_no"
            android:inputType="text"
            android:layout_width="0dp"
             android:layout_height="wrap_content"
            android:layout_gravity="left|center_vertical"
            android:layout_weight="1"
            android:hint="@string/hint_no"
            android:singleLine="true" >

            <requestFocus />
        EditText>

    LinearLayout>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/stu_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="@string/stu_name" />

        <EditText
            android:id="@+id/edit_name"
            android:inputType="text"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center_vertical"
            android:layout_weight="1"
            android:hint="@string/hint_name"
            android:singleLine="true" >
        EditText>

    LinearLayout>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/stu_sex"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="@string/stu_sex" />

        <EditText
            android:id="@+id/edit_sex"
            android:layout_width="0dp"
             android:layout_height="wrap_content"
            android:layout_gravity="left|center_vertical"
            android:layout_weight="1"
            android:hint="@string/hint_sex"
            android:singleLine="true" >
        EditText>

    LinearLayout>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/stu_birthday"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="@string/stu_birthday" />

        <EditText
            android:id="@+id/edit_birthday"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center_vertical"
            android:layout_weight="1"
            android:inputType="date"
            android:hint="@string/hint_birthday"
            android:singleLine="true" >
        EditText>

    LinearLayout>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/stu_class"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="@string/stu_class" />

        <EditText
            android:id="@+id/edit_class"
            android:inputType="text"
            android:layout_width="0dp"
             android:layout_height="wrap_content"
            android:layout_gravity="left|center_vertical"
            android:layout_weight="1"
            android:hint="@string/hint_class"
            android:singleLine="true" >
        EditText>

    LinearLayout>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal"
        >

        <TextView
            android:id="@+id/stu_email"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="@string/stu_email" />

        <EditText
            android:id="@+id/edit_email"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center_vertical"
            android:layout_weight="1"
            android:inputType="textEmailAddress"
            android:hint="@string/hint_email"
            android:singleLine="true" >
        EditText>

    LinearLayout>
    
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:gravity="right|bottom"
        >
        
        <Button 
            android:id="@+id/btn_enregister"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/btn_enregister"
            style="?android:attr/buttonStyle"
            android:onClick="click_enregister"/>
        <Button 
            android:id="@+id/btn_restting"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/btn_resetting"
            style="?android:attr/buttonStyle"
            android:onClick="click_restting"/>
    LinearLayout>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/stu_result"
            android:layout_width="match_parent"
            android:layout_height="20dp"
            android:text="@string/stu_result" />
        
        <TextView
            android:id="@+id/txt_result"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:singleLine="false" />

    LinearLayout>
    

LinearLayout>

MainActivity.java
package com.example.shiyan0301;

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

public class MainActivity extends Activity {
     
	
	private EditText edit_no;
	private EditText edit_name;
	private EditText edit_sex;
	private EditText edit_birthday;
	private EditText edit_class;
	private EditText edit_email;
	private TextView result;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
     
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		edit_no = (EditText)findViewById(R.id.edit_no);
		edit_name = (EditText)findViewById(R.id.edit_name);
		edit_sex = (EditText)findViewById(R.id.edit_sex);
		edit_birthday = (EditText)findViewById(R.id.edit_birthday);
		edit_class = (EditText)findViewById(R.id.edit_class);
		edit_email = (EditText)findViewById(R.id.edit_email);
		result = (TextView)findViewById(R.id.txt_result);
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
     
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	//点击事件方式一:指定Button的onClick属性方式
	
	public void click_enregister(View v) {
     
		String txt = "学号:" + edit_no.getText().toString() + "\t姓名:" + edit_name.getText().toString()
					+ "\t性别:" + edit_sex.getText().toString() + "\t出生年月:" + edit_birthday.getText().toString()
					+ "\t专业班级:" + edit_class.getText().toString() + "\t电子邮箱:" + edit_email.getText().toString();
    	result.setText(txt);
    }
	
	public void click_restting(View v) {
     
		edit_no.setText("");
		edit_name.setText("");
		edit_sex.setText("");
		edit_birthday.setText("");
		edit_class.setText("");
		edit_email.setText("");
    	result.setText("");
    }

}

实验 3 Android常用基本控件(一)_第1张图片

3.简易计算器

string.xml
<string name="title">计算器string>
    
    <string name="num_1">运算数一:string>
    <string name="num_2">运算数二:string>
    <string name="result">运算结果:string>
    
    <string name="add">加(+)string>
    <string name="sub">减(-)string>
    <string name="mul">乘(×)string>
    <string name="div">除(÷)string>
    <string name="empty">清空string>
    
    <string name="hint_num_1">请输入数一string>
    <string name="hint_num_2">请输入数二string>
activity_main.xml
<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: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:orientation="vertical"
    android:gravity="center" >
	
    <TextView
        android:id="@+id/txt_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="22sp"
        android:layout_marginBottom="20dp"
        android:text="@string/title"
    />
    
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        
        <TextView
            android:id="@+id/txt_num1"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="@string/num_1"
        />
        
        <EditText 
            android:id="@+id/edit_num1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:layout_weight="1"
            android:hint="@string/hint_num_1"/>
        
    LinearLayout>
    
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        
        <TextView
            android:id="@+id/txt_num2"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="@string/num_1"
        />
        
        <EditText 
            android:id="@+id/edit_num2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:layout_weight="1"
            android:hint="@string/hint_num_2"/>
        
    LinearLayout>
    
    
    
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        
        <TextView
            android:id="@+id/txt_result"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="@string/result"
        />
        
        <EditText 
            android:id="@+id/edit_result"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:enabled="false"
            
            />
        
    LinearLayout>
    
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        >
        
        <Button 
            android:id="@+id/btn_add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/add"
            style="?android:attr/buttonStyle"/>
        
        <Button
            android:id="@+id/btn_sub"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/sub"
            style="?android:attr/buttonStyle"/>
        
        <Button 
            android:id="@+id/btn_mul"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/mul"
            style="?android:attr/buttonStyle"/>
        
        <Button 
            android:id="@+id/btn_div"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/div"
            style="?android:attr/buttonStyle"/>
        
    LinearLayout>
    
    <Button 
        android:id="@+id/btn_empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/empty"
        android:layout_gravity="right"/>
    

LinearLayout>

MainActivity.java
package com.example.shiyan0303;

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 edit_num1,edit_num2;
	EditText edit_result;
	Button btn[] = new Button[5];

	@Override
	protected void onCreate(Bundle savedInstanceState) {
     
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		edit_num1 = (EditText)findViewById(R.id.edit_num1);
		edit_num2 = (EditText)findViewById(R.id.edit_num2);
		edit_result = (EditText)findViewById(R.id.edit_result);
		btn[0] = (Button)findViewById(R.id.btn_add);
		btn[1] = (Button)findViewById(R.id.btn_sub);
		btn[2] = (Button)findViewById(R.id.btn_mul);
		btn[3] = (Button)findViewById(R.id.btn_div);
		btn[4] = (Button)findViewById(R.id.btn_empty);
		
		for(int i = 0; i < btn.length; i++) {
     
			btn[i].setOnClickListener(new cmomputeListened(i));;
		}
	}
	
	class cmomputeListened implements OnClickListener {
     
		
		int num;
		float num1,num2;
		
		public cmomputeListened(int i) {
     
			num = i;
		}

		@Override
		public void onClick(View v) {
     
			if(num == 0) {
     
				num1 = Float.valueOf(edit_num1.getText().toString()).intValue();
				num2 = Float.valueOf(edit_num2.getText().toString()).intValue();
				edit_result.setText(String.valueOf(num1 + num2));
			}else if(num == 1) {
     
				num1 = Float.valueOf(edit_num1.getText().toString()).intValue();
				num2 = Float.valueOf(edit_num2.getText().toString()).intValue();
				edit_result.setText(String.valueOf(num1 - num2));
			}else if (num == 2) {
     
				num1 = Float.valueOf(edit_num1.getText().toString()).intValue();
				num2 = Float.valueOf(edit_num2.getText().toString()).intValue();
				edit_result.setText(String.valueOf(num1 * num2));
			}else if(num == 3){
     
				num1 = Float.valueOf(edit_num1.getText().toString()).intValue();
				num2 = Float.valueOf(edit_num2.getText().toString()).intValue();
				if(num2 == 0) {
     
					Toast.makeText(MainActivity.this, "除数不能为0", Toast.LENGTH_SHORT).show();
				}else {
     
					edit_result.setText(String.valueOf(num1 / num2));
				}
			}else {
     
				edit_num1.setText("");
				edit_num2.setText("");
				edit_result.setText("");
			}
		}
	}
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
     
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

实验 3 Android常用基本控件(一)_第2张图片

总结

提示:请勿全部搬迁,稍作修改稍作修改稍作修改稍作修改啊啊啊啊~~~~~

你可能感兴趣的:(安卓)