我试图将EditText的String值传递给线程中的方法,方法是“channel.basicpublish”。我在方法“basicpublish”中尝试了很多方法,比如Edittext.gettext()。tostring或声明全局变量(覆盖上面)并将它们传递给局部变量,但另一方面我得到一个空字符串,尽管没有错误。我也试过捆绑/意图,但没有什么对我有用(给出错误)。以下代码将起作用,因为我传递一个普通字符串(Message =“HelloRabbitmq”)。那么如何将一个字符串从edittext传递到一个线程中的“basicpublish”?
public class HandlerActivity extends Activity {
/** Called when the activity is first created. */
TextView tv;
EditText etv;
String QUEUE_NAME="bye";
String EXCHANGE_NAME="logs";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.out);
etv = (EditText) findViewById(R.id.out2);
etv.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
// Perform action on key press
Toast.makeText(HandlerActivity.this, etv.getText(), Toast.LENGTH_SHORT).show();
String Kamran = etv.getText().toString();
return true;
}
return false;
}
});
final Handler handler=new Handler();
final Runnable r=new Runnable()
{
public void run()
{
tv.append("Hello");
}
};
handler.postDelayed(r, 1000);
Thread thread = new Thread()
{
@Override
public void run() {
try {
while(true) {
sleep(1000);
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("192.168.2.3");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "fanout", true);
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
String message = "Hello Rabbitmq";
channel.basicPublish( "logs",QUEUE_NAME, null, message.getBytes());
channel.close();
connection.close();
handler.postDelayed(r, 1000);
}
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
};
thread.start();
}
}
答案 0 :(得分:0)
您是否尝试在 etv 上设置 TextWatcher 并设置String成员变量,假设 etvValue ,其值为< em> etv 在 afterTextChanged 回调?然后,将 etvValue 传递给basicPublish可能会有效......
答案 1 :(得分:0)
类变量应该可以正常工作......
我也进行了一些更改,但可以随意将其更改回原来,但除了EditText视图中的文本之外,代码中似乎还存在其他一些缺陷。
类似的东西:
public class HandlerActivity extends Activity {
private TextView tv;
private EditText etv;
private String QUEUE_NAME = "bye";
private String EXCHANGE_NAME = "logs";
private String message = "";
private Handler handler;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
handler = new Handler();
tv = (TextView) findViewById(R.id.out);
etv = (EditText) findViewById(R.id.out2);
etv.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
Toast.makeText(HandlerActivity.this, etv.getText().toString(), Toast.LENGTH_SHORT).show();
message = etv.getText().toString();
return true;
}
return false;
}
});
}
@Override
public void onStart() {
super.onStart();
handler.postDelayed(publish, 1000);
}
private Runnable publish = new Runnable() {
public void run() {
try {
//You should probably not have this as "true", you should always have some way to bail out of this.
//Technically even if you kill this activity it may never stop.
while(true) {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("192.168.2.3");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "fanout", true);
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
channel.basicPublish("logs", QUEUE_NAME, null, message.getBytes());
channel.close();
connection.close();
//Why are you running the this loop again? Uncomment if thats REALLY what you want
//handler.postDelayed(publish, 1000);
}
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
};