课本例子3-5与3-6的实现

一、3-5的实现
a.布局代码

Main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".MainActivity"
    android:gravity="center"
>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <Button
            android:id="@+id/btn1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="打开普通对话框" />

        <Button
            android:id="@+id/btn2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="打开登录对话框" />
    </LinearLayout>
</LinearLayout>

login.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/login">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="UserName"/>
    <EditText
        android:id="@+id/et_inputUserName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入您的用户名"
        android:textColorHint="#95A1AA"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Password"/>
    <EditText
        android:id="@+id/et_inputPassWord"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入您的密码"
        android:textColorHint="#95A1AA"/>

</LinearLayout>

b.逻辑代码的实现
1).ProcessDialog控件已经淘汰,故不再使用
2).按钮和对话框所使用的监听接口是不同的,前者是View后者是DialogInterface
3).虽然没什么用,姑且还是把账号的判断也加上了。

以下为实现代码:

package com.example.a3_5;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;


import android.widget.Toast;


public class MainActivity extends AppCompatActivity {

    //ProgressDialog mydialog;
    Button btn1,btn2;
    LinearLayout login;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn1=findViewById(R.id.btn1);
        btn2=findViewById(R.id.btn2);

        btn1.setOnClickListener(new Click());
        btn2.setOnClickListener(new Click());
    }

    class Click implements View.OnClickListener {

        AlertDialog.Builder dialog= new AlertDialog.Builder(MainActivity.this);

        @Override
        public void onClick(View args){
            if(args==btn1)
            {
                dialog.setTitle("警告");
                dialog.setIcon(R.drawable.icon1);
                dialog.setMessage("本项操作可能导致信息泄露");
                dialog.setPositiveButton("确定",new okClick());
                dialog.create();
                dialog.show();
            }else if(args==btn2)
            {
                login=(LinearLayout) getLayoutInflater().inflate(R.layout.login,null);
                dialog.setTitle("用户登录").setMessage("请输入用户名和密码").setView(login);
                dialog.setPositiveButton("确定",new loginClick());
                dialog.setNegativeButton("退出",new exitClick());
                dialog.setIcon(R.drawable.icon2);
                dialog.create();
                dialog.show();
            }
        }
    }

    class okClick implements DialogInterface.OnClickListener{
        @Override
        public void onClick(DialogInterface dialog,int which){
            dialog.cancel();
        }
    }
    class loginClick implements DialogInterface.OnClickListener{
        EditText txtName,txtPass;
        @Override
        public void onClick(DialogInterface dialog,int which){
            txtName=login.findViewById(R.id.et_inputUserName);
            txtPass=login.findViewById(R.id.et_inputPassWord);
            if(txtPass.getText().toString().equals("admin")&&txtName.getText().toString().equals("wilson"))
            {
                Toast.makeText(getApplicationContext(),"登录成功",Toast.LENGTH_SHORT).show();
            }else
            {
                Toast.makeText(getApplicationContext(),"账号或密码错误",Toast.LENGTH_SHORT).show();
            }
        }
    }
    class exitClick implements DialogInterface.OnClickListener{
        @Override
        public void onClick(DialogInterface dialog,int which){
            MainActivity.this.finish();
        }
    }
}

二、3-6的实现
老师要求我们在3-6的基础上把选择的日期和时间对话框在TextView中显示出来,这个难度其实并不大,只需要我们在课本的原有到吗基础上,增添两个TextView并设置相应的setText方法即可。于是我们小组的成员开始进行代码的设计与操作,但是在这个过程中依然遇到了一些问题,在多次实践中才找出问题所在,并成功解决,以下是代码过程中遇到的问题:
(一)android onDateSet方法默认的month设置的初始值是从0开始

1.首先原先按照书本上的代码进行实现:

public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { 
m_year = year;
m_month = monthOfYear;
m_day = dayOfMonth;
}

将日期设置为1月2日,结果显示:
在这里插入图片描述
因此需要将代码更改成:

public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { 
m_year = year;
m_month = monthOfYear+1;
m_day = dayOfMonth;
}

就可以正常显示我们需要的结过。
2.收获:刚开始设置的时候,并没有认真看清楚这个系统默认的问题,更注重的确认修改日期后是否有在TextView中显示更新,所以对日期是随意地设置,也没有注意到显示的结果与我们预期的不符。在后面经过一位小组成员的指正,更改了这个小错误。明白了在代码设置的过程中,要注意系统默认的初始值是否和我们日常的使用习惯一致,如果不一致,要及时对代码进行更正,使结果达到我们想要的结果

(二)TextView的setText方法的需要设置在onDateSet方法中
1.一开始没有意识到这个方法设置所放位置的重要性,将方法放置在了date.show的后面:

//创建日期对话框对象
                DatePickerDialog date = new DatePickerDialog(MainActivity.this, dateListener, m_year, m_month, m_day);
                date.setTitle("日期对话框");
                date.show();
                txt1.setText("您当前设置的日期是:\n" + m_year + "年" + m_month + "月" + m_day + "日");

然后实际实现的时候发现出现了以下问题,第一次设置完日期“2月13日”确认之后,日期显示的是默认设置的值,而不是当前设置的日期:
在这里插入图片描述
接着再次点击按钮,设置“3月13日”并点击确认后,退出设置,发现日期发生了更新,但是显示的是上一次设置的结果,而最新设置的结果并没有显示出来
在这里插入图片描述
于是发现设置日期出现的bug,设置完日期之后,需要再点击一次“设置当前日期”按钮,日期才会进行更新,后面小组成员内进行了讨论,才发现是代码实现的位置设置的问题,于是我们把代码放置再了onDateSet方法里面,让代码点击之后同时更新结果,则解决了问题:

public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        m_year = year;
                        m_month = monthOfYear+1;
                        m_day = dayOfMonth;
                        txt1.setText("您当前设置的日期是:\n" + m_year + "年" + m_month + "月" + m_day + "日");
                    }

2.收获:在前期代码设计的时候,一定要提前考虑好他们的逻辑实现顺序,才能够实现自己想要的结果,否则即使代码本身没有问题,还是有可能出现运行结果和我们所需要的需求不一样的情况。

(三)源代码
1.Main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    >
<Button
    android:id="@+id/datebtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/date"
    android:textSize="20sp"
    />
    <TextView
        android:id="@+id/datetext"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:gravity="center"/>
    <Button
        android:id="@+id/timebtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/time"
        android:textSize="20sp"/>
    <TextView
        android:id="@+id/timetext"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:gravity="center"
        />
</LinearLayout>

2.string.xml

<resources>
    <string name="app_name">My Application</string>
    <string name="date">设置当前日期</string>
    <string name="time">设置当前时间</string>
</resources>

3.实现代码

package com.example.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
    Button btn1,btn2;
    TextView txt1,txt2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1=(Button)findViewById(R.id.datebtn);
        btn2=(Button)findViewById(R.id.timebtn);
        txt1=(TextView)findViewById(R.id.datetext);
        txt2=(TextView)findViewById(R.id.timetext);
        btn1.setOnClickListener(new mClick());
        btn2.setOnClickListener(new mClick());
    }
    class mClick implements OnClickListener{
        int m_year=2019;
        int m_month=1;
        int m_day=1;
        int m_hour=12,m_minute=1;
        @Override
        public void onClick(View v) {

            if (v==btn1) {  //设置日期监听器
                OnDateSetListener dateListener = new OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        m_year = year;
                        m_month = monthOfYear+1;
                        m_day = dayOfMonth;
                        txt1.setText("您当前设置的日期是:\n" + m_year + "年" + m_month + "月" + m_day + "日");
                    }

                };
                //创建日期对话框对象
                DatePickerDialog date = new DatePickerDialog(MainActivity.this, dateListener, m_year, m_month, m_day);
                date.setTitle("日期对话框");
                date.show();

            }
                if(v==btn2){
                    //设置日期监听器
                    OnTimeSetListener timeListener=new OnTimeSetListener() {
                        @Override
                        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                            m_hour=hourOfDay;
                            m_minute=minute;
                            txt2.setText("您当前设置的时间是:\n"+m_hour+"点"+m_minute+"分");
                        }
                    };
                    //创建时间对话框对象
                    TimePickerDialog d=new TimePickerDialog(MainActivity.this,timeListener,m_hour,m_minute,true);
                    d.setTitle("时间对话框");
                    d.show();

                }


        }

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值