通过WhatsApp发送Textviews
问题描述:
我需要通过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();
}
}
}
什么做我必须做的?
感谢
答
更改此
String both = textview2 + "-" + textview3;
到
String both = textview2.getText().toString() + "-" + textview3.getText().toString();
+0
它的工作原理,但如果textview为null,则应用程序崩溃。 –
答
不能与任何应用共享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();
}
* #texttext.getText()。toString()。trim(); * – Sanoop
您无法发送视图。只有文字。 –