当前位置:   article > 正文

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

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;
    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/227321
推荐阅读
相关标签
  

闽ICP备14008679号