AlertDialog或者Dialog显示的内容异常

在Android中显示一个Dialog,为了性能更优化,会遵循这样一个机制。现在缓存中查找是否有包含该DialogID的对话框,如果有的话直接调用;反之就重新创建一个。

该方法对于性能优化比较有用,但是对于我们每次显示对话框都要求显示不同内容的需求来说就有点麻烦了。

方法就是在创建这个Dialog之前先将他从缓存中移除。如下:

    	switch(id)
    	{
    	case DIALOG_OPERATION_MENU:
			return new AlertDialog.Builder(this).setTitle("操作")
    				.setIcon(R.drawable.ic_launcher)
    				.setItems(R.array.operation_menu, ocl).create();
    		//重命名文件
    	case DIALOG_RENAME: 
    		return new AlertDialog.Builder(this).setTitle("重命名")
    				.setView(mRename).setPositiveButton("确定", ocl)
    				.setNegativeButton("取消", ocl).create();
    		//分享文件
    	case DIALOG_SHARE:
    		shareAPK();
    		break;
    		//显示详情
    	case DIALOG_DETAIL:
    		return showFileDetail();
    	}
    	return null;
    }

这里面都是显示一个Dialog,在显示每个Dialog方法前都会调用onPrepareDialog方法。我们就重写这个方法,从缓存中移除该dialog,如下:

	protected void onPrepareDialog(int id, Dialog dialog) {
	// TODO Auto-generated method stub
    	switch(id)
    	{
    	case DIALOG_OPERATION_MENU:
    	case DIALOG_RENAME:
    	case DIALOG_SHARE:
    	case DIALOG_DETAIL:
    		removeDialog(id);
    		break;
    	}
    	super.onPrepareDialog(id, dialog);
	}

这样每次显示新的Dialog都会调用onPrepareDialog,都会从缓存中删除该Dialog。这样就会重新运行一遍创建Dialog的流程,这样我们写在创建Dialog中的代码就会起作用。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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 ConnectTask().execute(ip, String.valueOf(port)); } }); Button sendMessageButton = findViewById(R.id.sendMessageButton); sendMessageButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showSendMessageDialog(); } }); } private class ConnectTask extends AsyncTask<String, Void, Void> { @Override protected Void doInBackground(String... params) { String ip = params[0]; int port = Integer.parseInt(params[1]); 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; int tnum = 0; tnum = number; out.println(tnum); } @Override public void onStartTrackingTouch(SeekBar seekBar) {} @Override public void onStopTrackingTouch(SeekBar seekBar) {} }); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } } @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) { new Thread(new Runnable() { @Override public void run() { out.println(message); } }).start(); } } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }) .create() .show(); }代码有问题,能帮我修改一下吗
最新发布
06-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值