【问题标题】:Sharing intent should check if textview is empty共享意图应检查 textview 是否为空
【发布时间】:2016-05-05 01:25:45
【问题描述】:

我在我的应用程序中实现了共享意图按钮。我在文本视图中有字符串,但它并不总是填充任何文本。所以,我想检查它是否为空。如果字符串为空,则共享意图应忽略/删除/获取该字符串并转到下一个字符串。我尝试以编程方式执行此操作,但没有成功。我怎样才能做到这一点?我需要一些帮助:

public void buttonClick2(View view) {


            TextView textView1 = (TextView)findViewById(R.id.word);
            TextView textView2 = (TextView)findViewById(R.id.definition);
            TextView textView3 = (TextView)findViewById(R.id.definition2);
            TextView textView4 = (TextView)findViewById(R.id.definition3);




            boolean hasDefinition2 = false;
        if (textView3 !=null){
            String text3 = textView3.getText().toString();
            if(text3 !=null && !text3.isEmpty()){
                hasDefinition2 = true;
            }
        }

        String def2 ="";
        if(hasDefinition2){
            def2 +="\n Definition 2: "+textView3.getText().toString();
        }


        boolean hasDefinition3 = false;
        if (textView4 !=null){
            String text4 = textView4.getText().toString();
            if(text4 !=null && !text4.isEmpty()){
                hasDefinition3 = true;
            }
        }

        String def3 ="";
        if(hasDefinition3){
            def3 +="\n Definition 3: "+textView4.getText().toString();
        }   
            Intent shareIntent1 = new Intent(android.content.Intent.ACTION_SEND);
            shareIntent1.setAction(android.content.Intent.ACTION_SEND);
            shareIntent1.setType("text/plain");
            shareIntent1.putExtra(android.content.Intent.EXTRA_SUBJECT, "Word"); //<=adding blank quotes

            shareIntent1.putExtra(Intent.EXTRA_TEXT, textView1.getText().toString()+
                    "\n"+Definition: +textView2.getText().toString()+
                    "\n"+def2+
                    "\n"+def3+
                    "\n"+"\n"+"@2016 Dictionary"+
                    "\n"+"Visit us:");    
            startActivity(Intent.createChooser(shareIntent1, "Share"));





        }

【问题讨论】:

    标签: android android-intent android-sharing


    【解决方案1】:

    您可以尝试以下方法来检查 textView3/4 是否为空:-

    String extra =textView1.getText().toString()+
                    "\n"+Definition: +textView2.getText().toString(); 
    
    if (textView3 !=null){
        String text = textView3.getText().toString();
        if(text !=null && !text.isEmpty()){
            extra +="\n Definition 2:"+ textView3.getText().toString(); 
        }
    }
    
    if (textView4 !=null){
        String text = textView4.getText().toString();
        if(text !=null && !text.isEmpty()){ 
           extra +="\n Definition 3:"+ textView4.getText().toString();
        }
    } 
    
    extra += "\n"+"\n"+"@2016 Dictionary"+
             "\n"+"Visit us:"
    
    shareIntent.putExtra(Intent.EXTRA_TEXT, extra);
    startActivity( ... )
    

    也可以考虑使用 StringBuilder 而不是 String 来做额外的。

    【讨论】:

    • 好的,成功了!但是当文本为空时,当我将它分享给某人时,共享意图会提供空间,就好像那里有一个空白字符串。我不想要这个空间。我想要这个“删除” textView 当为空时,就好像它不存在并转到下一个字符串。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-08
    • 1970-01-01
    • 2012-03-25
    相关资源
    最近更新 更多