简体   繁体   中英

How to get text from EditText in Config Layout Android?

I have config layout contains EditText is serverString when user input.

<EditText
    android:id="@+id/serverURL"
    android:inputType="textUri"
    android:hint="@string/server_url_hint"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="20" >

    <requestFocus />
</EditText>

And code to class Configuration_Activity.java to get values. This is my code:

private void onCreate(Bundle savedInstanceState) {
    serverUrl = (EditText)findViewById(R.id.serverURL);
}

But I don't know how to get values when user input and user press button back, text will saved, and this code like:

serverUrl.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
    }
});

In other class I want to get values of EditText to process, this class extends from Services , I can't using findViewById to get values. This is my code:

public class Zing extends Service {
    final String serverUrlString = "http://www.myserver.com";
}

In this code, the variable serverUrlString is not flexible. So I want to when user input text in Configuration this will save then serverUrlSTring get this text to continues process.

UPDATED:

Send to @Eduardo Yáñez Parareda:

I tried your updated code. It has much different in my application. First, I try your code it can't find IBinder , bindService , I try change to like:

serverUrl.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
    Context c = getApplicationContext();
    final String serverStringURL = serverUrl.getText().toString();
    preferenceManager.setServerUrlText(serverStringURL);
    Intent serverStartIntent = new Intent(c, CallRecordService.class);
    serverStartIntent.putExtra("serverStringURL",serverStringURL.toString());
    startActivity(serverStartIntent);
}
});

But in class Zing extends Services I can't getExtra(values). Because this class extends Services.

Thanks.

If you want to start your service with an Intent and send extra values, override the method onStart, then get the intent and related information:

// Or you can extend from IntentService (API 23)

public class Zing extends Service {
    public int onStartCommand(Intent intent, int flags, int startId) {
        // It'd be better call another method passing the Intent...
        Bundle extras = intent.getExtras();
        return START_STICKY;
    }
}
 serverUrl.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            String ServerStr = s.toString();
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM