按钮单击时将用户输入字符串保存到变量

时间:2015-03-06 15:26:05

标签: java pubnub

在我的应用程序中,用户将在可编辑的文本字段中输入他们想要订阅的频道。单击订阅按钮后,我的方法中用于订阅用户的变量应更新为用户选择的变量,然后将显示Toast通知用户他们订阅了此频道。

在底部,我得到用户在EditText字段中输入的字符串值,但我不认为我把它放在正确的位置。有人可以看看它,让我知道我做错了什么?让我知道,如果我不清楚或遗漏了什么,我可以尝试更好地解释。谢谢!

public class MainActivity extends ActionBarActivity {

private Button channelSubscribeButton;
private EditText subscribeChannelEditText;

//Declare variable
String subscribeChannel = null;


//-------------------------Access PubNub API-------------------------//
Pubnub pubnub = new Pubnub("pub-c-940c4776-36ff-425f-9677-f1c904a9d57b", "sub-c-cf42b292-a8bf-11e4-85d5-0619f8945a4f");


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    channelSubscribeButton = (Button) findViewById(R.id.subscribe_button);
    subscribeChannelEditText = (EditText) findViewById(R.id.channel_name);



    //-------------------------Subscribe to a Channel to Receive Messages-------------------------//



    try {
//THIS IS WHERE I AM INPUTTING THE VARIABLE THAT CONTAINS THE USER INPUTTED TEXT//
//I AM GETTING ERROR THAT SAYS IT CAN'T RESOLVE THE SYMBOL 'subscribeChannel'//

        pubnub.subscribe(subscribeChannel, new Callback() {

                    @Override
                    public void connectCallback(String channel, Object message) {
                        Log.d("PUBNUB", "SUBSCRIBE : CONNECT on channel:" + channel
                                + " : " + message.getClass() + " : "
                                + message.toString());
                    }

                    @Override
                    public void disconnectCallback(String channel, Object message) {
                        Log.d("PUBNUB", "SUBSCRIBE : DISCONNECT on channel:" + channel
                                + " : " + message.getClass() + " : "
                                + message.toString());
                    }

                    public void reconnectCallback(String channel, Object message) {
                        Log.d("PUBNUB", "SUBSCRIBE : RECONNECT on channel:" + channel
                                + " : " + message.getClass() + " : "
                                + message.toString());
                    }

                    @Override
                    public void successCallback(String channel, Object message) {
                        Log.d("PUBNUB", "SUBSCRIBE : " + channel + " : "
                                + message.getClass() + " : " + message.toString());
                    }

                    @Override
                    public void errorCallback(String channel, PubnubError error) {
                        Log.d("PUBNUB", "SUBSCRIBE : ERROR on channel " + channel
                                + " : " + error.toString());
                    }
                }
        );
    } catch (PubnubException e) {
        Log.d("PUBNUB", e.toString());
    }

public void onChannelButtonClick(View view) {

    subscribeChannel = String.valueOf(subscribeChannelEditText.getText());

    String yourSubscribeChannel = "Subscribed to the " + subscribeChannel + " Channel";

    Toast.makeText(this, yourSubscribeChannel, Toast.LENGTH_LONG).show();

    return subscribeChannel;
}

1 个答案:

答案 0 :(得分:3)

您的subscribeChannel变量仅在onChannelButtonClick方法的范围内可见。与实例变量一起在外面声明它。

public void onChannelButtonClick(View view) {    
    //here you set it and then it's lost.
    String subscribeChannel = String.valueOf(subscribeChannelEditText.getText());    
    String yourSubscribeChannel = "Subscribed to the " + subscribeChannel + " Channel";    
    Toast.makeText(this, yourSubscribeChannel, Toast.LENGTH_LONG).show();    
}

取而代之的是:

//declare it.
String subscribeChannel = null;

然后在你的方法上分配值。

public void onChannelButtonClick(View view) {    
    //assign it.
    subscribeChannel = String.valueOf(subscribeChannelEditText.getText());    
    String yourSubscribeChannel = "Subscribed to the " + subscribeChannel + " Channel";    
    Toast.makeText(this, yourSubscribeChannel, Toast.LENGTH_LONG).show();    
}