我正在尝试在我的服务器计算机和我的 android 智能手机之间创建一个通信系统。我终于得到了很好的工作成果。但是,我对 android 视图有疑问。具体来说,我试图从一个没有扩展的类中使用文本视图的方法“.setText()”。我将我的 textview 对象传递给该类,但方法“textview.setText()”可以正常工作,并且不会对 UI 产生任何影响。让我把代码直接写下来,我想你会更清楚地理解并有机会尝试代码。
public class NetworkingActivity extends Activity {
public static String message;
public static EditText editext;
public static TextView textview;
private Connector connector;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void buttonClicked(View v){
editext = (EditText) findViewById(R.id.editText1);
textview = (TextView) findViewById(R.id.textview);
message = editext.getText().toString();
connector = new Connector(NetworkingActivity.this);
connector.start();
}
public void yenile(String respond){
textview.setText(respond);
editext.setText("");
}
public void yenileClicked(View v){
textview.setText(Connector.respond);
editext.setText("");
}
public static class Connector extends Thread{
private static final int PORT = 3588;
private static final String IpAddress = "XXX.XXX.XXX.XXX"; // of course X's have a int value
public static String respond;
private NetworkingActivity activity;
public Connector(NetworkingActivity activity){
this.activity = activity;
}
public void run() {
try {
Socket socket = new Socket(IpAddress, PORT);
PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
Scanner scanner = new Scanner(socket.getInputStream());
writer.println(NetworkingActivity.message);
respond = scanner.nextLine();
activity.yenile(respond);
socket.close();
} catch (Exception e) {
}
}
}
}
最后,我还要告诉你一件事。方法yenileClicked() 由xml 文件中的代码“android:onClick="yenileClicked"”通过按钮分配。问题是其他类无法执行几乎相同的方法yenile();