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

Save user input string to variable on button click

在我的应用程序中,用户将在可编辑的文本字段中输入他们想要订阅的频道。单击订阅按钮后,放置在我的订阅用户方法中的变量应更新为用户选择的变量,然后将出现一个 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;
}

您的 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();    
}