Android 点击按钮弹出输入框和选择框

参考

1、输入框:

final EditText inputServer = new EditText(this);

inputServer.setFilters(new InputFilter[]{new InputFilter.LengthFilter(50)});

AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setTitle(“个性点的签名”).setIcon(android.R.drawable.ic_dialog_info).setView(inputServer)

.setNegativeButton(“取消”, null);

builder.setPositiveButton(“确定”, new DialogInterface.OnClickListener() {undefined

public void onClick(DialogInterface dialog, int which) {undefined

String _sign = inputServer.getText().toString();

if(_sign!=null && !_sign.isEmpty())

{undefined

signature_txt.setText(_sign);

}

else

{undefined

Toast.makeText(MemberActivity.this,“签名为空”,Toast.LENGTH_SHORT).show();

}

}

});

builder.show();

2、选择弹出框

final String[] items = {“保密”,“男”,“女”};

new AlertDialog.Builder(MemberActivity.this)

.setTitle(“请选择”)

.setItems(items, new DialogInterface.OnClickListener() {undefined

public void onClick(DialogInterface dialog,

int which) {undefined

TextView item_value = (TextView)memberInfo_tab_list_2.getChildAt(2).findViewById(R.id.item_value);

item_value.setText(items[which]);

}

}).show();

我的代码如下

package com.meetdilse.store.ui.activity;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;

import android.os.Bundle;
import android.text.InputFilter;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.meetdilse.store.R;
import com.meetdilse.store.base.BaseActivity;
import com.meetdilse.store.base.OkHttp;
import com.meetdilse.store.data.Myself;
import com.meetdilse.store.utils.ToastUtil;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


public class ModInfActivity extends BaseActivity {//baseactivity继承于原来那个,然后全部的activity都继承于baseactivity
    private ProgressDialog progressDialog;
    private TextView tvBirthday1;
    private Calendar calendar;// 用来装日期的
    private DatePickerDialog dialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(com.meetdilse.store.R.layout.activity_modify_information);
        TextView tvNickname = findViewById(R.id.tv_nickname);
        TextView tvNickname1 = findViewById(R.id.tv_nickname1);

        TextView tvSex = findViewById(R.id.tv_sex);
        TextView tvSex1 = findViewById(R.id.tv_sex1);
        TextView tvBirthday = findViewById(R.id.tv_birthday);
        tvBirthday1 = findViewById(R.id.tv_birthday1);
        TextView tvSign = findViewById(R.id.tv_sign);
        TextView tvSign1 = findViewById(R.id.tv_sign1);


        tvNickname.setOnClickListener(new View.OnClickListener() {//点击按键弹出输入弹框
            @Override
            public void onClick(View view) {
                final EditText inputServer = new EditText(ModInfActivity.this);
                inputServer.setFilters(new InputFilter[]{new InputFilter.LengthFilter(50)});//设置最多只能输入50个字符
                AlertDialog.Builder builder = new AlertDialog.Builder(ModInfActivity.this);//构建对话框,一个对话框,上面有输入框了,然后就还要有取消和确认键
                builder.setIcon(android.R.drawable.ic_dialog_info).setView(inputServer)
                        .setNegativeButton("取消", null);//设置取消键
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {//设置确认键,且对确认键进行监听
                    public void onClick(DialogInterface dialog, int which) {
                        String sign = inputServer.getText().toString();//点击确认后获取输入框的内容
                        if (sign != null && !sign.isEmpty()) {//如果内容不为空,这个判断是为了防止空指针
                            tvNickname1.setText(sign);//settext
                            Map<String, String> params = new HashMap<>();
                            params.put("nickname", sign);
                            OkHttp.create(ModInfActivity.this).updateSelfInfo(params).enqueue((Call, httpRes) -> {//发送okhttp请求,将修改的信息上传到服务器
                                if (httpRes.isSuccessful()) {
                                    Log.v("updateSelfInfo", httpRes.toString());

                                }
                            });


                        } else {
                            Toast.makeText(ModInfActivity.this, "签名为空", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                builder.show();//启动
            }
        });

        tvSex.setOnClickListener(new View.OnClickListener() {//点击按键弹出选择弹框
            @Override
            public void onClick(View view) {
                final String[] items = {"男", "女"};//选择的内容

                new AlertDialog.Builder(ModInfActivity.this)//新建对话框
                        .setNegativeButton("取消", null)//取消键
                        .setTitle("请选择")//标题

                        .setItems(items, new DialogInterface.OnClickListener() {//设置选择的内容,且对选择的内容进行监听

                            public void onClick(DialogInterface dialog, int which) {

//                                TextView item_value = (TextView)memberInfo_tab_list_2.getChildAt(2).findViewById(R.id.item_value);//绑定选项的那个控件
//
//                                item_value.setText(items[which]);//这个可以看看效果是怎么样的
//                                tvSex1.setText(items[which]);//自己绑定的控件显示选择到的内容
                                if (items[which] == "男") {//如果选择的是男的,就发送请求上传到服务器
                                    Map<String, String> params = new HashMap<>();
                                    params.put("sex", "1");
                                    OkHttp.create(ModInfActivity.this).updateSelfInfo(params).enqueue((Call, httpRes) -> {
                                        if (httpRes.isSuccessful()) {
                                            Log.v("updateSelfInfo", httpRes.toString());
                                            tvSex1.setText("男");
                                        }
                                    });
                                }
                                if (items[which] == "女") {
                                    Map<String, String> params = new HashMap<>();
                                    params.put("sex", "2");
                                    OkHttp.create(ModInfActivity.this).updateSelfInfo(params).enqueue((Call, httpRes) -> {
                                        if (httpRes.isSuccessful()) {
                                            Log.v("updateSelfInfo", httpRes.toString());
                                            tvSex1.setText("女");
                                        }
                                    });
                                }


                            }

                        }).show();
            }
        });
        tvSign.setOnClickListener(new View.OnClickListener() {//点击按键弹出输入弹窗
            @Override
            public void onClick(View view) {
                final EditText inputServer = new EditText(ModInfActivity.this);
                inputServer.setFilters(new InputFilter[]{new InputFilter.LengthFilter(50)});
                AlertDialog.Builder builder = new AlertDialog.Builder(ModInfActivity.this);
                builder.setIcon(android.R.drawable.ic_dialog_info).setView(inputServer)
                        .setNegativeButton("取消", null);
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        String sign = inputServer.getText().toString();
                        if (sign != null && !sign.isEmpty()) {
                            tvSign1.setText(sign);
                            Map<String, String> params = new HashMap<>();
                            params.put("signature", sign);
                            OkHttp.create(ModInfActivity.this).updateSelfInfo(params).enqueue((Call, httpRes) -> {
                                if (httpRes.isSuccessful()) {
                                    Log.v("updateSelfInfo", httpRes.toString());

                                }
                            });


                        } else {
                            Toast.makeText(ModInfActivity.this, "签名为空", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                builder.show();
            }
        });


        tvBirthday.setOnClickListener(new View.OnClickListener() {//点击按键弹出日期选择器
            @Override
            public void onClick(View view) {
                progressDialog = new ProgressDialog(ModInfActivity.this);
                progressDialog.show();//可能是系统进度条的显示
                progressDialog.dismiss();//可能是系统进度条的消失
                calendar = Calendar.getInstance();//获取日历对象
                Calendar ninth = Calendar.getInstance();//这里又获取一个
                ninth.set(Calendar.MONTH, 1970);//这里的1970是用来限制日历的,不能选择小于1970的年份
                dialog = new DatePickerDialog(ModInfActivity.this, new DatePickerDialog.OnDateSetListener() {//日期选择器

                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        System.out.println("年-->" + year + "月-->" + monthOfYear + "日-->" + dayOfMonth);

                        Calendar calendar = Calendar.getInstance();//获取日期对象
                        calendar.set(Calendar.YEAR, year);
                        calendar.set(Calendar.MONTH, monthOfYear);
                        calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);//设置年月日,点击到的就赋值到这里来


                        if (year>1970){
                        Long timeInMillis = calendar.getTimeInMillis() / 1000;//除1000是因为服务器以毫秒算的来着
                        int monYear = monthOfYear + 1;//系统月是数组形式的,要加1才是正确的月份
                        tvBirthday1.setText(year + "/" + monYear + "/" + dayOfMonth);
                        Log.v("timeInMillis", String.valueOf(timeInMillis));

                        Map<String, String> params = new HashMap<>();//创建hashmap对象
                        params.put("birthday", String.valueOf(timeInMillis));//放数据进去
                        OkHttp.create(ModInfActivity.this).updateSelfInfo(params).enqueue((call, httpRes) -> {
                            Log.v("updateSelfInfobriday", httpRes.toString());
                            if (httpRes.isSuccessful()) {
                                Log.v("updateSelfInfobriday", httpRes.toString());
                            }
                            ToastUtil.out(httpRes.getMsg());//有这个吐司工具类才能这样子用
                        });
                    }else {ToastUtil.out("请选择大于1970的年份");}
                    }

                }, calendar.get(Calendar.YEAR), calendar
                        .get(Calendar.MONTH), calendar
                        .get(Calendar.DAY_OF_MONTH));
                dialog.show();//最后点show



            }
        }



        );



        OkHttp.create(this).getSelfInfo().enqueue((call, httpRes) -> {//在点击事件里面settext后,退出界面又进来就会失效了的,这里重新拉一遍数据
            Log.d("abc123", httpRes.toString());
            if (httpRes.isSuccessful()) {
                Myself mySelf = httpRes.dataFromObj(Myself.class);
                tvNickname1.setText(String.valueOf(mySelf.getNickname()));
                if (mySelf.getSex() == 1) {
                    tvSex1.setText("男");
                }
                if (mySelf.getSex() == 2) {
                    tvSex1.setText("女");
                }

                Date day = new Date();//获取现在的日期
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//格式化
                String now = df.format(day);//现在的日期格式化获得字符串形式
                try {
                    Date now1 = df.parse(now);//字符串转日期
                    long second = mySelf.getBirthday();//获取服务器日期,秒单位
                    String dateString = secondToDate(second, "yyyy-MM-dd hh:mm:ss");//将秒格式化为日期
                    Log.v("birthday111", dateString);
                    String stime = dateString.substring(0, 10);//格式化好后获得字符串数据,然后裁剪0到9的数据出来
                    tvBirthday1.setText(stime);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                tvSign1.setText(String.valueOf(mySelf.getSignature()));
            }
        });

    }

    private String secondToDate(long second, String patten) {//这个方法可以让秒转为日期

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(second * 1000);//转换为毫秒
        Date date = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat(patten);
        String dateString = format.format(date);
        return dateString;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值