代码改变世界

第九次作业,之前第七次作业2名字错了

2019-11-14 18:20  123赵乾  阅读(111)  评论(0编辑  收藏  举报
package com.example.myapplication;

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


public class MainActivity extends Activity implements View.OnClickListener  {

    private ContactDao mContactDao;
    private Button mBtnadd;
    private Button mBtnupdata;
    private Button mBtnquery;
    private Button mBtndelete;
    private EditText mName;
    private EditText mPhone;
    private TextView mTv;
    private String name="";
    private String phone="";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContactDao = new ContactDao(this);
        mName = (EditText) findViewById(R.id.name);
        mPhone = (EditText) findViewById(R.id.phone);
        mBtnadd =(Button) findViewById(R.id.btn_add);
        mTv = (TextView) findViewById(R.id.tv);
        mBtnupdata =(Button) findViewById(R.id.btn_updata);
        mBtnquery =(Button) findViewById(R.id.btn_query);
        mBtndelete =(Button) findViewById(R.id.btn_delete);
        mBtnadd.setOnClickListener(this);
        mBtnupdata.setOnClickListener(this);
        mBtnquery.setOnClickListener(this);
        mBtndelete.setOnClickListener(this);
        mTv.setText(mContactDao.Select(name));

    }
    @Override
    public void onClick(View v) {
        name = mName.getText().toString();
        phone = mPhone.getText().toString();
        switch (v.getId()) {
            case R.id.btn_add:
                if(mContactDao.Insert(name, phone)){
                    Toast.makeText(this, "数据添加成功", 0).show();
                    mTv.setText(mContactDao.Select(name));
                }else {
                    Toast.makeText(this, "数据添加失败", 0).show();
                }

                break;
            case R.id.btn_query:
                mTv.setText(mContactDao.Select(name));
                break;
            case R.id.btn_delete:
                if(mContactDao.Delete(name)) {
                    Toast.makeText(this, "数据删除成功", 0).show();
                    mTv.setText(mContactDao.Select(name));
                }else {
                    Toast.makeText(this, "数据删除失败", 0).show();
                }
                break;
            case R.id.btn_updata:
                if(mContactDao.Updata(name, phone)) {
                    Toast.makeText(this, "数据修改成功", 0).show();
                    mTv.setText(mContactDao.Select(name));
                }else {
                    Toast.makeText(this, "数据修改失败", 0).show();
                }
                break;

        }
    }
}
<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"
    android:orientation="vertical"
    tools:context="com.example.myapplication.MainActivity" >

    <LinearLayout
        android:layout_marginTop="100dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用  户"/>
        <EditText
            android:id="@+id/name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入用户姓名"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="电  话"/>
        <EditText
            android:id="@+id/phone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入手机号码"/>
    </LinearLayout>
    <LinearLayout
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/btn_add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="增加"/>
        <Button
            android:id="@+id/btn_updata"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="修改"/>
        <Button
            android:id="@+id/btn_query"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="查询"/>
        <Button
            android:id="@+id/btn_delete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="删除"/>
    </LinearLayout>
    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

</LinearLayout>
<resources>

    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>
</resources>