我在尝试在 textview 上使用 setText 显示一串文本时遇到了真正的问题。我对 web 服务的所有调用,我获取数据的函数,如果我使用 EditText 来显示字符串,一切都会正确显示,但我宁愿文本只是出现在屏幕上,而不是作为文本出现在 EditText 中是信息而不是用户可以编辑的东西。
此代码适用于 EditText -
package xxx.refusecollectionday;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class SingleListItem extends Activity{
/** Called when the activity is first created. */
private static String NAMESPACE = "http://xxx";
private static String URL = "http://xxx/Service.asmx";
private static String SOAP_ACTION = "http://xxxx";
private static String METHOD_NAME1 = "xxx";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.single_list_item_view);
Intent intent = getIntent();
String uprn = intent.getStringExtra(AndroidListViewActivity.UPRN).trim();
String displayText = "";
//Initialize soap request + add parameters
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);
//Use this to add parameters
request.addProperty("UPRN",uprn);
//Declare the version of the SOAP request
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
try {
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
//this is the actual part that will call the webservice
androidHttpTransport.call(SOAP_ACTION, envelope);
// Get the SoapResult from the envelope body.
SoapObject result = (SoapObject)envelope.bodyIn;
if(result != null) {
String strXML = result.getProperty(0).toString();
strXML = sortXMLString(strXML);
}
}else{
showBalloonPopUp("No Response");
}
}catch(Exception e) {
e.printStackTrace();
}
displayText = displayText + "\nTo contact the Customer Service Centre:\n";
EditText txtCollectionResults = (EditText) findViewById(R.id.collectionresults);
// displaying selected product name
txtCollectionResults.setText(displayText);
//showAlertDialog(displayText);
}
public void showBalloonPopUp(String popUpMessage){
Toast.makeText(this, popUpMessage, Toast.LENGTH_SHORT).show();
}
public void showAlertDialog(String alertMessage){
AlertDialog ad = new AlertDialog.Builder(this).create();
ad.setCancelable(false); // This blocks the 'BACK' button
ad.setMessage(alertMessage);
ad.setButton(-1, "OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
ad.show();
}
public String sortXMLString(String stringToCorrect){
// do stuff
return stringToCorrect;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText android:id="@+id/collectionresults"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="15dip"
android:padding="10dip"
android:textColor="#000000"
android:maxLines="50"
android:editable="false" />
</LinearLayout>
但是如果我将 EditText 更改为 xml 中的 TextView,则如下
TextView txtCollectionResults = (TextView) findViewById(R.id.collectionresults);
// displaying
txtCollectionResults.setText(displayText);
不显示任何文字