[英]How to get text from EditText in Config Layout Android?
當用戶輸入時,我的配置布局包含EditText
為serverString
。
<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>
並編寫代碼以將Configuration_Activity.java
類獲取值。 這是我的代碼:
private void onCreate(Bundle savedInstanceState) {
serverUrl = (EditText)findViewById(R.id.serverURL);
}
但是我不知道當用戶輸入並按下按鈕時如何獲取值,文本將被保存,該代碼類似於:
serverUrl.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
在其他我想獲取EditText
值的類中,該類extends from Services
,我無法使用findViewById
來獲取值。 這是我的代碼:
public class Zing extends Service {
final String serverUrlString = "http://www.myserver.com";
}
在此代碼中,變量serverUrlString
不靈活。 所以我想當用戶在“配置”中輸入文本時,將保存然后serverUrlSTring
獲取此文本以繼續處理。
更新:
發送至@EduardoYáñezParareda:
我嘗試了更新的代碼。 它與我的應用程序有很大不同。 首先,我嘗試找不到IBinder
, bindService
代碼, bindService
嘗試更改為:
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);
}
});
但是在class Zing extends Services
我不能getExtra(values)。 因為此類擴展了Services。
謝謝。
如果要使用Intent啟動服務並發送其他值,請重寫onStart方法,然后獲取Intent和相關信息:
//或者可以從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();
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.