在android中:如何使Listview中的字符串启用复制

时间:2017-02-09 06:40:15

标签: java android string listview copy

public class mychatorderdetails extends ActionBarActivity {
    private MyApplication app;
    private String order_id;
    private String orderdate;
    private String cust_name;
    private String cust_address;
    private String cust_pincode;
    private String cust_mobile;
    private String action;
    private String UPI;
    private Button bt;
    private OrderDetailsAdapter oa;
    private ListView lv;
    private ArrayList<HashMap<String, String>> messageList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mychatorderdetails);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        messageList = new ArrayList<HashMap<String, String>>();
        app=((MyApplication) getApplicationContext());

        Bundle extras = getIntent().getExtras();

        if(extras != null) {
            order_id = extras.getString("order_id");
            orderdate = extras.getString("orderdate");
            cust_name = extras.getString("cust_name");
            cust_address = extras.getString("cust_address");
            cust_pincode = extras.getString("cust_pincode");
            cust_mobile=extras.getString("cust_mobile");
            UPI=extras.getString("UPI");
            action=extras.getString("action"); //setaccepted, setdelivered
        }

        bt = (Button) findViewById(R.id.btnAction);
        switch (action)
        {
            case "setdelivered":{
                bt.setText("Delivered");
                break;
            }
            case "setaccepted":{
                bt.setText("Accept");
                break;
            }
            default:
        }

        try {
            String dpart = orderdate.substring(0, 10);
            String tpart = orderdate.substring(11, 19);
            orderdate = dpart.substring(8,10);
            orderdate+="/"+dpart.substring(5,7);
            orderdate+="/"+dpart.substring(0,4);
            orderdate+=" "+tpart;
        }
        catch (Exception e)
        {
            Log.d(getString(R.string.app_name), e.getMessage());
        }
        HashMap<String, String> row = new HashMap<String, String>();
        row.put("type", "text");
        row.put("details", "Order ID:"+order_id);
        messageList.add(row);
        HashMap<String, String> rowd = new HashMap<String, String>();
        rowd.put("type", "text");
        rowd.put("details", "Order Date:"+orderdate);
        messageList.add(rowd);
        HashMap<String, String> row1 = new HashMap<String, String>();
        row1.put("type", "text");
        row1.put("details",cust_name);
        messageList.add(row1);
        HashMap<String, String> row2 = new HashMap<String, String>();
        row2.put("type", "text");
        row2.put("details",cust_address);
        messageList.add(row2);
        HashMap<String, String> row3 = new HashMap<String, String>();
        row3.put("type", "text");
        row3.put("details","Pin:"+cust_pincode);
        messageList.add(row3);
        HashMap<String, String> row4 = new HashMap<String, String>();
        row4.put("type", "text");
        row4.put("details","Mob:"+cust_mobile);
        messageList.add(row4);
        HashMap<String, String> row5 = new HashMap<String, String>();
        row5.put("type", "text");
        row5.put("details","UPI:"+UPI);
        messageList.add(row5);
        oa=new OrderDetailsAdapter(mychatorderdetails.this,messageList);
        lv = (ListView) findViewById(R.id.lstOrderDetails);
        lv.setAdapter(oa);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                try {
                    HashMap<String, String> temp = new HashMap<String, String>();
                    temp = messageList.get(position);
                    if(temp.get("type")=="image")
                    {
                        //Uri uri = Uri.parse(temp.get("details"));
                        showImage(temp.get("details"));
                    }

                } catch (Exception e) {
                    Log.d(getString(R.string.app_name), e.getMessage());
                }
            }
        });

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                    try {
                        JSONArray det = app.getMyChatOrderDetails(order_id);
                        for (int i = 0; i < det.length(); i++) {
                            JSONObject cat = det.getJSONObject(i);
                            HashMap<String, String> row = new HashMap<String, String>();
                            if(!TextUtils.isEmpty(cat.getString("msg_image").toString()))
                            {
                                row.put("type", "image");
                                row.put("details",cat.getString("msg_image").toString());
                            }
                            else
                            {
                                row.put("type", "text");
                                String mfv=cat.getString("msg_for_vendor").toString();
                                if(mfv.equals("1"))
                                {
                                    row.put("details","Customer: "+cat.getString("msg_text").toString());
                                }
                                else{
                                    row.put("details","Me: "+cat.getString("msg_text").toString());
                                }

                            }
                            messageList.add(row);
                        }

                        oa.notifyDataSetChanged();

                    } catch (JSONException e) {
                        Log.d(getString(R.string.app_name), e.getMessage());
                    } catch (Exception e) { //connection timeout
                        Log.d(getString(R.string.app_name), e.getMessage());
                        Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
                    }
            }
        });
    }

    public void showImage(String imageUri) {
        Dialog builder = new Dialog(this);
        builder.requestWindowFeature(Window.FEATURE_NO_TITLE);
        builder.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialogInterface) {
                //nothing;
            }
        });
        try {
            ImageView imageView = new ImageView(this);
            Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(imageUri).getContent());
            if (bitmap != null) {
                imageView.setImageBitmap(bitmap);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(450, 400);
                imageView.setLayoutParams(layoutParams);
                imageView.setVisibility(View.VISIBLE);
                builder.addContentView(imageView, new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                builder.show();
            }
        }
        catch (Exception e)
        {
            Log.d(getString(R.string.app_name), e.getMessage());
        }
    }

    public void doAction(View v)
    {
        switch (action)
        {
            case "setdelivered":{
                if(app.setOrderDelivered(order_id)) {
                    Toast.makeText(this,"Order marked as delivered...",Toast.LENGTH_LONG).show();
                    bt.setVisibility(View.INVISIBLE);
                }
                break;
            }
            case "setaccepted":{
                if(app.acceptChatOrder(order_id)){
                    Toast.makeText(this,"Order accepted...",Toast.LENGTH_LONG).show();
                    bt.setVisibility(View.INVISIBLE);
                }
                break;
            }
            default:
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_mychatorderdetails, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        Intent intent;
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_logout) {
            app = ((MyApplication) getApplicationContext());
            app.logOut();
            intent = new Intent(mychatorderdetails.this, LoginActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
            mychatorderdetails.this.startActivity(intent);
        }
        mychatorderdetails.this.finish();

        return super.onOptionsItemSelected(item);
    }
}

以下是显示我的列表项目的代码。我希望我的第5行项目,即UPI在长按时启用复制。我该怎么做呢?请编辑我的代码以实现相同目的。

2 个答案:

答案 0 :(得分:0)

在ListView的row_layout中,只需将属性android:textIsSelectable添加到允许复制的TextView即可。

答案 1 :(得分:0)

更改布局文件:将以下属性添加到TextView

android:textIsSelectable="true"

OR

在Java类中编写此行以编程方式设置它:

myTextView.setTextIsSelectable(true);

复制文字:

tv.setOnLongClickListener(new View.OnLongClickListener() {

    @Override
    public boolean onLongClick(View v) {
        ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData clip = ClipData.newPlainText("", tv.getText());
        clipboard.setPrimaryClip(clip);
        Toast.makeText(context, "Copied to clipboard", Toast.LENGTH_SHORT).show();
        return true;
    }
});

确保您已导入 android.content.ClipboardManager 而非android.text.ClipboardManager