• 第五次安卓作业(计算器和增删改查)


    <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"
    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/tv2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/et2"
    android:layout_centerVertical="true"
    android:gravity="center"
    android:text="显示计算结果"
    android:textStyle="italic" />
    <EditText
    android:id="@+id/et3"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tv2"
    android:layout_alignLeft="@+id/et1"
    android:ems="10" >
    <requestFocus />
    </EditText>
    <EditText
    android:id="@+id/et1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/et3"
    android:layout_alignLeft="@+id/tv1"
    android:hint="请输入数值"
    android:ems="10" />
    <EditText
    android:id="@+id/et2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/et1"
    android:layout_alignLeft="@+id/tv1"
    android:hint="请输入数值"
    android:ems="10" />
    <Button
    android:id="@+id/sum"
    android:layout_width="152dp"
    android:layout_height="wrap_content"
    android:editable="false"
    android:text="+" />

    <Button
    android:id="@+id/reduce"
    android:layout_width="152dp"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/sum"
    android:text="-"
    android:textSize="20sp" />
    <Button
    android:id="@+id/ride"
    android:layout_width="152dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/sum"
    android:text="X" />

    <Button
    android:id="@+id/except"
    android:layout_width="152dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/reduce"
    android:layout_toRightOf="@+id/ride"
    android:text="/" />

    </RelativeLayout>


    package com.example.a;

    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;
    import android.widget.Toast;

    public class MainActivity extends Activity {

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

    public void click(View v) {
    TextView tv2 = (TextView) findViewById(R.id.tv2);
    EditText et1 = (EditText) findViewById(R.id.et1);
    EditText et2 = (EditText) findViewById(R.id.et2);

    int num1,num2,result=0;
    num1=Integer.parseInt(et1.getText().toString());
    num2=Integer.parseInt(et2.getText().toString());

    switch (v.getId()) {
    case R.id.sum:
    result=num1+num2;
    break;
    case R.id.reduce:
    result=num1-num2;
    break;
    case R.id.ride:
    result=num1*num2;
    break;
    case R.id.except:
    result=num1/num2;
    break;
    }
    tv2.setText(result+"");
    }

    }

    增删改查::

    <?xml version="1.0" encoding="UTF-8"?>

    <LinearLayout android:padding="16dp" android:orientation="vertical" android:background="@drawable/bg" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:android="http://schemas.android.com/apk/res/android">


    <LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:layout_marginTop="130dp">

    <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="18sp" android:text="姓 名 :"/>

    <EditText android:layout_height="wrap_content" android:layout_width="match_parent" android:textSize="16sp" android:hint="请输入姓名" android:id="@+id/et_name"/>

    </LinearLayout>


    -<LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:layout_marginBottom="10dp">

    <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="18sp" android:text="年 龄:"/>

    <EditText android:layout_height="wrap_content" android:layout_width="match_parent" android:textSize="16sp" android:hint="输入年龄" android:id="@+id/et_age"/>

    </LinearLayout>


    -<LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent">

    <Button android:background="#B9B9FF" android:layout_height="wrap_content" android:layout_width="0dp" android:textSize="18sp" android:text="添加" android:id="@+id/btn_add" android:onClick="add" android:layout_weight="1" android:layout_marginRight="2dp"/>

    <Button android:background="#DCB5FF" android:layout_height="wrap_content" android:layout_width="0dp" android:textSize="18sp" android:text="查询" android:id="@+id/btn_query" android:onClick="search" android:layout_weight="1" android:layout_marginRight="2dp"/>

    <Button android:background="#E6CAFF" android:layout_height="wrap_content" android:layout_width="0dp" android:textSize="18sp" android:text="修改" android:id="@+id/btn_update" android:onClick="update" android:layout_weight="1" android:layout_marginRight="2dp"/>

    <Button android:background="#ACD6FF" android:layout_height="wrap_content" android:layout_width="0dp" android:textSize="18sp" android:text="删除" android:id="@+id/btn_delete" android:onClick="delete" android:layout_weight="1"/>

    </LinearLayout>


    -<ScrollView android:layout_height="wrap_content" android:layout_width="match_parent">

    <TextView android:layout_height="wrap_content" android:layout_width="match_parent" android:layout_marginTop="25dp" android:textSize="20sp" android:id="@+id/tv_show"/>

    </ScrollView>

    </LinearLayout>

    package com.example.login_add;

    import android.annotation.SuppressLint;
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.ContentValues;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.view.Menu;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.widget.Toast;

    public class MainActivity extends Activity {

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

    @SuppressLint("WrongConstant")
    public void add(View v) {
    StuOpenHelper helper = new StuOpenHelper(this);
    SQLiteDatabase db = helper.getWritableDatabase();
    String name = ((EditText) findViewById(R.id.et_name)).getText()
    .toString();
    int age = Integer.parseInt(((EditText) findViewById(R.id.et_age))
    .getText().toString());

    // 原生sql操作,
    // db.execSQL("insert into stuinfo (name,age) values('lisi',22)");
    // db.execSQL("insert into stuinfo (name,age) values(?,?)",new
    // Object[]{"kitty",30});
    db.execSQL("insert into stuinfo (name,age) values(?,?)", new Object[] {
    name, age });
    Toast.makeText(this, "ok", 0).show();

    }

    public void delete(View view) {
    StuOpenHelper helper = new StuOpenHelper(this);
    SQLiteDatabase db = helper.getWritableDatabase();
    db.execSQL("delete from stuinfo where _id=?", new Object[] { 2 });
    Toast.makeText(this, "删除成功", 0).show();

    }

    @SuppressLint("WrongConstant")
    public void update(View view) {
    StuOpenHelper helper = new StuOpenHelper(this);
    SQLiteDatabase db = helper.getWritableDatabase();
    db.execSQL("update stuinfo set name=? where _id=?", new Object[] {
    "micky", 3 });
    Toast.makeText(this, "修改成功", 0).show();

    }

    public void search(View view) {
    StuOpenHelper helper = new StuOpenHelper(this);
    SQLiteDatabase db = helper.getWritableDatabase();
    String s = "";
    Cursor cursor = db.rawQuery("select * from stuinfo", null);
    if (cursor.getCount() != 0) {

    while (cursor.moveToNext()) {
    s += cursor.getInt(0) + " " + cursor.getString(1) + " "
    + cursor.getInt(2) + " ";
    }
    }

    // Toast.makeText(this, s, 0).show();
    ((TextView) (findViewById(R.id.tv_show))).setText(s);

    }

    }

    package com.example.login_add;

    import android.content.Context;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteDatabase.CursorFactory;
    import android.database.sqlite.SQLiteOpenHelper;

    public class StuOpenHelper extends SQLiteOpenHelper {

    public StuOpenHelper(Context context) {
    super(context, "stu.db", null, 1);
    // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    db.execSQL("create table stuinfo(_id integer primary key autoincrement,name varchar(20),age integer)");

    }

    @Override
    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
    // TODO Auto-generated method stub

    }

    }

  • 相关阅读:
    用户模版
    VC让对话框显示就最大化
    C#中char[]与string之间的转换
    c#调用C++DLL参数对应
    解决:对 PInvoke 函数的调用导致堆栈不对称问题 <转载>
    git 命令的使用
    me 云面试
    Django 自定义模板语法
    Django session 源码流程
    云计算的介绍
  • 原文地址:https://www.cnblogs.com/sigure0428/p/14163828.html
Copyright © 2020-2023  润新知