共享意图应检查textview是否为空

时间:2016-05-05 01:25:46

标签: android android-intent android-sharing

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

<div class="statistics">
<span class="statistictable">


</span>
<span class="statistictable">
</span>
<span class="statistictable">

</span>
<span class="statistictable">

</span>
</div>

1 个答案:

答案 0 :(得分: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来获取额外的。

相关问题