共享意图应该检查 textview 是否为空
Sharing intent should check if textview is empty
我在我的应用程序中实现了共享意图按钮。我在文本视图中有字符串,但它并不总是充满任何文本。所以,我想检查它是否为空。如果一个字符串为空,共享意图应该 ignore/remove/get 骑这个字符串并转到下一个字符串。我试图以编程方式执行此操作,但没有成功。我怎样才能做到这一点?我需要一些帮助:
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"));
}
您可以尝试以下方法检查 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 作为额外的。
我在我的应用程序中实现了共享意图按钮。我在文本视图中有字符串,但它并不总是充满任何文本。所以,我想检查它是否为空。如果一个字符串为空,共享意图应该 ignore/remove/get 骑这个字符串并转到下一个字符串。我试图以编程方式执行此操作,但没有成功。我怎样才能做到这一点?我需要一些帮助:
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"));
}
您可以尝试以下方法检查 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 作为额外的。