builder.setPositiveButton("确认", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { try { lat = Double.parseDouble(edit.getText().toString()); // 存储数据 SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit(); editor.putString("lat", Double.toString(lat)); editor.apply(); } catch (NumberFormatException e) { MyToast.makeText(XbyActivity.this, "输入数据有误", Toast.LENGTH_SHORT, true).show(); // 返回上个页面 Intent intent = new Intent(); setResult(RESULT_OK, intent); finish(); } } });解释每一句

时间: 2024-02-10 08:35:11 浏览: 31
这段代码是在创建 AlertDialog.Builder 对象时为其设置“确认”按钮的点击事件监听器。具体解释如下: 1. `builder.setPositiveButton("确认", ...)`:设置对话框的“确认”按钮,并为其设置点击事件监听器。 2. `new DialogInterface.OnClickListener() {...}`:创建一个 DialogInterface.OnClickListener 对象,用于监听“确认”按钮的点击事件。 3. `public void onClick(DialogInterface dialog, int which) {...}`:重写 DialogInterface.OnClickListener 的 onClick 方法,当用户点击“确认”按钮时,会执行该方法内的代码。 4. `lat = Double.parseDouble(edit.getText().toString());`:获取用户在对话框中输入的纬度,并将其转换成 Double 类型的数据,赋值给变量 lat。 5. `SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();`:获取名为 "data" 的 SharedPreferences 对象的编辑器。 6. `editor.putString("lat", Double.toString(lat));`:将获取到的纬度数据以字符串形式存储到 SharedPreferences 中。 7. `editor.apply();`:提交 SharedPreferences 中的编辑操作。 8. `catch (NumberFormatException e) {...}`:捕获 NumberFormatException 异常,当用户输入的数据无法转换成 Double 类型时,会抛出该异常。 9. `MyToast.makeText(XbyActivity.this, "输入数据有误", Toast.LENGTH_SHORT, true).show();`:显示一个 Toast,提示用户输入的数据有误。 10. `Intent intent = new Intent();`:创建一个 Intent 对象。 11. `setResult(RESULT_OK, intent);`:设置当前 Activity 的返回结果为 RESULT_OK,表示操作成功。 12. `finish();`:结束当前 Activity,返回上一个 Activity。

相关推荐

public class MainActivity extends AppCompatActivity { private EditText ipEditText; private EditText portEditText; private TextView messageTextView; private BufferedReader in; private PrintWriter out; private Socket socket; private Handler handler = new Handler(); @SuppressLint("MissingInflatedId") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ipEditText = findViewById(R.id.ipEditText); portEditText = findViewById(R.id.portEditText); messageTextView = findViewById(R.id.messageTextView); Button connectButton = findViewById(R.id.connectButton); connectButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String ip = ipEditText.getText().toString(); int port = Integer.parseInt(portEditText.getText().toString()); new Thread(new Runnable() { @Override public void run() { try { socket = new Socket(ip, port); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new PrintWriter(socket.getOutputStream(), true); // 在主页面中的窗口中实时显示消息 while (true) { String message = in.readLine(); if (message == null) { break; } handler.post(new Runnable() { @Override public void run() { messageTextView.append(message + "\n"); } }); } } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }).start(); } }); Button sendMessageButton = findViewById(R.id.sendMessageButton); sendMessageButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showSendMessageDialog(); } }); } @Override protected void onDestroy() { super.onDestroy(); try { if (in != null) { in.close(); } if (out != null) { out.close(); } if (socket != null) { socket.close(); } } catch (IOException e) { e.printStackTrace(); } } private void showSendMessageDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); LayoutInflater inflater = getLayoutInflater(); View view = inflater.inflate(R.layout.dialog_send_message, null); final EditText messageEditText = view.findViewById(R.id.messageEditText); builder.setView(view) .setTitle("发送消息") .setPositiveButton("发送", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String message = messageEditText.getText().toString(); if (out != null) { out.println(message); } } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }) .create() .show(); } }能帮我将这段代码中的发送信息从主线程改成子线程吗

public void onBindViewHolder(@NonNull ViewHolder holder, int position) { PhotoDiary photoDiary = photoList.get(holder.getAdapterPosition()); holder.imageView.setImageBitmap(BitmapFactory.decodeFile(photoDiary.getPhotoPath())); holder.captionTextView.setText(photoDiary.getCaption()); holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(view.getContext(),PhotoDetailsActivity.class); intent.putExtra("title",photoDiary.getCaption()); intent.putExtra("path",photoDiary.getPhotoPath()); view.getContext().startActivity(intent); } }); holder.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext()); builder.setMessage("confirm"); builder.setCancelable(false); builder.setMessage("You really want to delete this diary?"); builder.setPositiveButton("confirm", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { //点击confirm按钮 new Thread(new Runnable() { @Override public void run() { PhotoDiaryDao diaryDao = AppDatabase.getInstance(view.getContext()).photoDiaryDao(); diaryDao.delete(photoDiary); } }).start(); photoList.remove(photoDiary); notifyDataSetChanged(); } }); builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { dialogInterface.dismiss(); } }); AlertDialog dialog = builder.create(); dialog.show(); return false; } }); }解释这段代码

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ipEditText = findViewById(R.id.ipEditText); portEditText = findViewById(R.id.portEditText); Button connectButton = findViewById(R.id.connectButton); connectButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String ip = ipEditText.getText().toString(); int port = Integer.parseInt(portEditText.getText().toString()); new Thread(new Runnable() { @Override public void run() { try { socket = new Socket(ip, port); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new PrintWriter(socket.getOutputStream(), true); // 获取SeekBar对象 SeekBar progressBar = findViewById(R.id.progressBar); // 添加OnSeekBarChangeListener监听器 progressBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // 计算百分比对应的数字 int number = progress * 10; out.println(number); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }).start(); } }); Button sendMessageButton = findViewById(R.id.sendMessageButton); sendMessageButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showSendMessageDialog(); } }); }主线程有问题,能帮我修改一下吗

最新推荐

recommend-type

计算机专业毕业设计范例845篇jsp2118基于Web停车场管理系统的设计与实现_Servlet_MySql演示录像.rar

博主给大家详细整理了计算机毕业设计最新项目,对项目有任何疑问(部署跟文档),都可以问博主哦~ 一、JavaWeb管理系统毕设项目【计算机毕设选题】计算机毕业设计选题,500个热门选题推荐,更多作品展示 计算机毕业设计|PHP毕业设计|JSP毕业程序设计|Android毕业设计|Python设计论文|微信小程序设计
recommend-type

Windows 10 平台 FFmpeg 开发环境搭建 博客资源

【FFmpeg】Windows 10 平台 FFmpeg 开发环境搭建 ④ ( FFmpeg 开发库内容说明 | 创建并配置 FFmpeg 项目 | 拷贝 DLL 动态库到 SysWOW64 目录 ) https://hanshuliang.blog.csdn.net/article/details/139172564 博客资源 一、FFmpeg 开发库 1、FFmpeg 开发库编译 2、FFmpeg 开发库内容说明 二、创建并配置 FFmpeg 项目 1、拷贝 dll 动态库到 C:\Windows\SysWOW64 目录 - 必须操作 特别关注 2、创建 Qt 项目 - C 语言程序 3、配置 FFmpeg 开发库 - C 语言项目 4、创建并配置 FFmpeg 开发库 - C++ 项目
recommend-type

基于 Spring Cloud 、Spring Boot、 OAuth2 的 RBAC 企业快速开发平台

基于 Spring Cloud 、Spring Boot、 OAuth2 的 RBAC 企业快速开发平台, 同时支持微服务架构和单体架构。提供对 Spring Authorization Server 生产级实践,支持多种安全授权模式。提供对常见容器化方案支持 Kubernetes、Rancher2 、Kubesphere、EDAS、SAE 支持
recommend-type

我赚100万的商业模式.pdf

我赚100万的商业模式
recommend-type

Python_编译器学习资源收集.zip

Python_编译器学习资源收集
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

深入了解MATLAB开根号的最新研究和应用:获取开根号领域的最新动态

![matlab开根号](https://www.mathworks.com/discovery/image-segmentation/_jcr_content/mainParsys3/discoverysubsection_1185333930/mainParsys3/image_copy.adapt.full.medium.jpg/1712813808277.jpg) # 1. MATLAB开根号的理论基础 开根号运算在数学和科学计算中无处不在。在MATLAB中,开根号可以通过多种函数实现,包括`sqrt()`和`nthroot()`。`sqrt()`函数用于计算正实数的平方根,而`nt
recommend-type

react的函数组件的使用

React 的函数组件是一种简单的组件类型,用于定义无状态或者只读组件。 它们通常接受一个 props 对象作为参数并返回一个 React 元素。 函数组件的优点是代码简洁、易于测试和重用,并且它们使 React 应用程序的性能更加出色。 您可以使用函数组件来呈现简单的 UI 组件,例如按钮、菜单、标签或其他部件。 您还可以将它们与 React 中的其他组件类型(如类组件或 Hooks)结合使用,以实现更复杂的 UI 交互和功能。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。