2016-05-05 5 views
-1

私のアプリケーションに意図の共有ボタンを実装しました。 textviewsに文字列がありますが、必ずしもテキストで埋められているとは限りません。だから、私はそれが空であるかどうかチェックしたいと思います。文字列が空の場合、共有目的はこの文字列を無視/削除/取得して次の文字列に移動する必要があります。私はプログラムでこれをやろうとしましたが、成功しませんでした。これどうやってするの?私はいくつかのhelpoが必要ですtextviewが空であるかどうかを共有することを意図してください。

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")); 





     } 

答えて

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の代わりの文字列を使用することを検討してください。

+0

しかし、テキストが空であれば、共有目的は、誰かにそれを共有するときに空白文字列があるかのようにスペースを与えます。私はこのスペースを望んでいません。私はこの "delete" textViewが空の場合、それが存在しないかのようにして、次の文字列に移動します。 –

+0

@HollyIkki、私の編集を参照してください。 –

+0

ありがとうございました!!!出来た!! :D –

関連する問題