Android 期末课堂代码总结

Posted Akaid_Tea

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 期末课堂代码总结相关的知识,希望对你有一定的参考价值。

yyt期末_android课堂总结

计算器的简单实现

题目:运用Android Studio编写一个简单计算器APP

  1. 对按钮功能的监听
  /**
     * 数字按钮监听器
     **/
    class clickNum implements OnClickListener {
        public void onClick(View v) {
            Button num = (Button) v;                     //获取用户点击的按钮对象
            String clickNum = num.getText().toString();  //获取用户点击的数字
            progress = showRst.getText().toString();     //获取结果的值并赋值给progress
            if(v == btn0){          //按0
                progress = dealRes(progress,clickNum);
            }else {                 //按1-9
                progress = dealRes(progress,clickNum);
            }
            showRst.setText(progress);
        }
    }

    /**
     * 功能按钮监听器
     *
     * 可以加“.”的情况
     * \\\\d+       \\\\d+\\+|-|/|\\*\\\\d+      \\\\d+\\\\.\\\\d+\\+|-|/|\\*\\\\d+
     *
     */
    class clickOther implements OnClickListener {
        public void onClick(View v) {
            Button num = (Button) v;
            String reg = "^(\\\\d+)|^(\\\\d+\\\\+|-|/|\\\\*\\\\d+)|^(\\\\d+\\\\.\\\\d+\\\\+|-|/|\\\\*\\\\d+)";
            String symbol = num.getText().toString();    //获取用户点击的符号
            progress = showRst.getText().toString();     //获取结果的值并赋值给progress
            //按清除按钮
            if(v == clear){
                progress = "0";
                currentSymbol = "";
            //按等号按钮
            } else if(v == btnEq) {
                String []arr = progress.split("\\\\+|-|/|\\\\*");
                if(progress.split("\\\\+|-|/|\\\\*").length != 1) {
                    progress = splitStr(progress,currentSymbol);
                }
            //按“.”按钮
            } else if ( v == btnDot) {
                //如果不符合可以加“.”的情况,则不变,否则加上“.”
                progress = !Pattern.matches(reg,progress)  ? progress : dealRes(progress,symbol);
            //按加减乘除按钮
            } else {
                //如果式子中存在“+*/”,进行运算;如果存在“-”且不是首位,进行运算
                Boolean a = progress.indexOf('+') != -1 || progress.indexOf('*') != -1 || progress.indexOf('/') != -1;
                Boolean b = progress.indexOf('-') != -1;
                Boolean c = progress.lastIndexOf('-') != 0;
                if (a) {
                    progress = splitStr(progress,currentSymbol);
                    currentSymbol = num.getText().toString();
                    progress = progress + currentSymbol;
                } else if (b && c) {
                    progress = splitStr(progress,currentSymbol);
                    currentSymbol = num.getText().toString();
                    progress = progress + currentSymbol;
                } else {
                    currentSymbol = num.getText().toString();
                    progress = progress + currentSymbol;
                }
            }
            showRst.setText(progress);
        }
    }
  1. 运算逻辑处理
 /**
     * 分割字符串,进行计算。并返回计算结果
     * 两个参数:完整式子,运算符号
     */
    public String splitStr (String str , String calSymbol) {
        String []strArray = new String[20];
        strArray = str.split("\\\\+|-|/|\\\\*");         //使用+-*/对式子进行分割
        Double res = 0.0;
        switch (calSymbol) {                                //根据运算符号选取操作
            case "+" : {
                if (strArray.length > 2) res = (- Double.parseDouble(strArray[1])) + Double.parseDouble(strArray[2]);
                else res = Double.parseDouble(strArray[0]) + Double.parseDouble(strArray[1]);
                currentSymbol = "";
                break;
            }
            case "-" : {
                if (strArray.length > 2) res = (- Double.parseDouble(strArray[1])) - Double.parseDouble(strArray[2]);
                else  res = str.indexOf('-') == 0 ? Double.parseDouble(str) : Double.parseDouble(strArray[0]) - Double.parseDouble(strArray[1]);
                currentSymbol = "";
                break;
            }
            case "*" : {
                if (strArray.length > 2) res = (- Double.parseDouble(strArray[1])) * Double.parseDouble(strArray[2]);
                else res = Double.parseDouble(strArray[0]) * Double.parseDouble(strArray[1]);
                currentSymbol = "";
                break;
            }
            case "/" : {
                if (strArray.length > 2) res = (- Double.parseDouble(strArray[1])) / Double.parseDouble(strArray[2]);
                else res = Double.parseDouble(strArray[0]) / Double.parseDouble(strArray[1]);
                currentSymbol = "";
                break;
            }
        }
        return String.valueOf(res);
    }

    /**
     * 判断字符串组成,并返回结果
     */
    public String dealRes (String str,String clickSymbol){
        String reValue = "";
        //如果字符串长度为0,直接返回点击的字符
        if (str.length() == 0) {
            reValue = clickSymbol;
        //如果字符串长度为为1,且等于0,且字符不为“.”,则返回点击的字符
        } else if(str.length() == 1 && Integer.parseInt(str) == 0 && clickSymbol != btnDot.getText().toString()) {
            reValue = clickSymbol ;
        //其他情况,直接拼接点击的字符字符
        } else {
            reValue = str + clickSymbol;
        }
        return  reValue;
    }

Intent的运用

题目:MainActivity.java和Main2Activity.java。MainActivity中输入姓名和密码,点击登录能够传递到对应的 Activity,并显示到其 TextView 组件上。name 是姓名文本编辑框变量,passwd 是密码文本编辑框变量,login 是登录按钮变量。

  1. MainActivity.java 源代码如下:
public class MainActivity extends AppCompatActivity {
	EditText name,passwd;
 	Button login,cancel;
	@Override
 	protected void onCreate(Bundle savedInstanceState) {
 	
	 	super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		name=findViewById(R.id.editText3);
		passwd=findViewById(R.id.editText4);
		login=findViewById(R.id.button);
		cancel=findViewById(R.id.button2);
		
		login.setOnClickListener (new View.OnClickListener() {
			@Override
			public void onClick(View v) {
			Intent intent=new Intent(MainActivity.this,Main2Activity.class)
			intent.putExtra("userName", name.getText().tostring().trim() ); 
			intent.putExtra("password", password.getText().tostring().trim() ); 
			start Activity(intent);
			}
		});
	}
}
  1. Main2Activity.java 源代码如下:
public class Main2Activity extends AppCompatActivity {
	 TextView textView;
	 @Override
	 protected void onCreate(Bundle savedInstanceState) {
		 super.onCreate(savedInstanceState);
		 setContentView(R.layout.activity_main2);
		 textView=findViewById(R.id.textView);
		 
		 Intent intent = getIntent() ;
		 String userName = intent.getStringExtra(“userName”) ;
		 String password = intent.getStringExtra(“password”) ;
		 textView.setText ("用户名;"+userName+"\\n"+"密码:"+password);
 	}
}

ListView运用

题目:利用 ListView 组件设计如下图所示的界面效果,点击 ListView 中的某一项时,会将这一项的文字信息显示在 Toast 窗口中。
在这里插入图片描述

public class MainActivity extends AppCompatActivity {
	ListView listView;
	String []title=new String[]{"数据","分层","位置","视频","通知","购物车","信息","点赞"};
	int []image=new int[]{R.drawable.img01,R.drawable.img02,R.drawable.img03,
						R.drawable.img04,R.drawable.img05,R.drawable.img06,
						R.drawable.img07,R.drawable.img08};

 @Override
 protected void onCreate(Bundle savedInstanceState) {
	 super.onCreate(savedInstanceState);
	 setContentView(R.layout.activity_main);
	 
	 listView = view.findViewById(R.id.listView);
	 List<Map<String,Object>> list=new ArrayList<>();
	 
	 for(int i=0;i<title.length;i++) {
		 Map<String,Object> map=new HashMap<>();
		 map.put("title",title[i]);
		 map.put("image",image[i]);
		 list.add(map);
	 }
	 
	 SimpleAdapter adapter=newSimpleAdapter(MainActivity.this, list ,
R.layout.item,
	 new String[]{"title","image"},
	 new int[]{R.id.textView,R.id.imageView});
	listView.setAdapter(adapter); 
	 listView. listView.setAdapter(adapter); (newAdapterView.OnItemClickListener() {
	@Override
	public void onItemClick(AdapterView<?> parent, View 
		view, int position, long id) {
			Toast.makeText(MainActivity.this, listView.setAdapter(adapter),Toast.LENGTH_SHORT).show();
		 }
	 });
	 }
}

广播消息Broadcast

题目:MainActivity.java,文本框中输入信息,点击“发送广播”按钮,广播接收器(MyBroadcastReceiver.java)的处理方式是将接收到的信息显示在 Toast 窗口中,完成下面的程序填空。

public class MainActivity extends AppCompatActivity{
	private EditText entryText ;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		entryText = (EditText)findViewById(R.id.entry); 
	}
	public void send(View view){
		Intent intent = new Intent(MainActivity.this,MyBroadcastReceiver.class);
		intent.putExtra("message",entryText.getText().toSting().trim());
		sentBroadcast(intent);
	}
}
public class MyBroadcastReceiver extends BroadcastReceiver{
	@Override
	public void onReceive(Context context, Intent intent) {
		String msg = intent.getStringExtra(“message”);
		Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
	}
}

数据库SQLite

题目:现在需要管理的学生信息如下:学号、姓名、身份证号、性别、家庭住址、监护人、监护人电话。要求用 SQLite 数据库存储,并定义对数据库中的数据表进行增删改查功能的Student_Database 类,学生实体类 Student.java 已经提供。

public class Student{
	private int id;
	private String username;
	private String personalid;
	private String sex;
	private String address;
	private String parent;
	private String parentphone;
	public Student (int id,String username,String personal_id,String sex,String address,String parent,String parentphone){
	this.id=id;
	this.username=username;
 	this.personalid=personal_id;
	this.sex=sex;
	this.address=address;
 	this.parent=parent;
 	this.parentphone=parentphone;
	}
	//关于属性的 set 和 get 函数省略
	 // ……
}
public final class Student_Database extends SQLiteOpenHelper{
	static int counter=20180001;
		public Student_Database(Context context){
			super(context,"student-db.db",null,3);
		}
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
			try {
				db.execSQL("drop table if exists "+"student");
				onCreate(db);
			} 
			catch (SQLException e) {
			e.printStackTrace();
			}
		}
	}
	@Override
	public void onCreate(SQLiteDatabase sqLiteDatabase) {
		String sql="create table student(id integer primary key autoincrement," +
		 "username text not null," +
		 "personalid text not null," +
		 "sex text not null," +
		 "address text not null," +
		 "parent text not null," +
		 "parentphone text not null)";
		sqLiteDatabase.execSQL(sql)
	 }
	public void adddata(SQLiteDatabase sqLiteDatabase,String name,String personalid,String sex,String address,String parent,String parentphone){
		 counter&

以上是关于Android 期末课堂代码总结的主要内容,如果未能解决你的问题,请参考以下文章

软件工程期末项目总结

HTML5期末大作业:餐饮美食网站设计——咖啡(10页) HTML+CSS+JavaScript 学生DW网页设计作业成品 web课程设计网页规划与设计 咖啡网页设计 美食餐饮网页设计...(代码片段

是否有在单个活动中处理多个片段的 Android 设计模式?

开发总结自动期末评教工具:技术功能说明部分代码展示演示视频

第二周课堂测验总结

2019年下C语言课程学期总结