2017-06-09 62 views
-3

我需要通過whatsapp方法將佈局的所有textview作爲字符串發送,但是如果textview爲null,則必須發送空白文本。通過WhatsApp發送Textviews

這是我的代碼:

public void sendButton(View view) { 
    // Do something in response to button 

    PackageManager pm = getPackageManager(); 
    try { 

     TextView textview3 = (TextView) findViewById(R.id.textAmor3); 
     TextView textview2 = (TextView) findViewById(R.id.textAmor2); 

     Intent waIntent = new Intent(Intent.ACTION_SEND); 
     waIntent.setType("text/plain"); 

     if (textview3 == null) { 


     } else { 

     } 




     String both = textview2 + "-" + textview3; 

     PackageInfo info = pm.getPackageInfo("com.whatsapp", 
     PackageManager.GET_META_DATA); 
     //Check if package exists or not. If not then code 
     //in catch block will be called 
     waIntent.setPackage("com.whatsapp"); 

     waIntent.putExtra(Intent.EXTRA_TEXT, both); 

     startActivity(Intent.createChooser(waIntent, "Share with")); 

    } catch (PackageManager.NameNotFoundException e) { 
     Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT) 
       .show(); 
    } 
} 
} 

什麼做我必須做的?

感謝

+0

* #texttext.getText()。toString()。trim(); * – Sanoop

+0

您無法發送視圖。只有文字。 –

回答

0

更改此

String both = textview2 + "-" + textview3;

String both = textview2.getText().toString() + "-" + textview3.getText().toString();

+0

它的工作原理,但如果textview爲null,則應用程序崩潰。 –

0

不能與任何應用共享Textviews。你可以分享在Textviews上寫的文字。所以得到文本使用textview3.getText().toString(),然後將其傳遞到應用程序的意圖。

Intent waIntent = new Intent(Intent.ACTION_SEND); 
waIntent.setType("text/plain"); 

try { 
    PackageInfo info = pm.getPackageInfo("com.whatsapp", 
    PackageManager.GET_META_DATA); 
    //Check if package exists or not. If not then code 
    //in catch block will be called 
    waIntent.setPackage("com.whatsapp"); 

    waIntent.putExtra(Intent.EXTRA_TEXT, "textString here"); 

    startActivity(Intent.createChooser(waIntent, "Share with")); 

} catch (PackageManager.NameNotFoundException e) { 
    Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT) 
      .show(); 
} 
相關問題