【发布时间】:2016-07-25 00:42:31
【问题描述】:
当用户输入时,我的配置布局包含 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 获取此文本以继续处理。
更新:
发送给@Eduardo Yáñez Parareda:
我尝试了您更新的代码。它在我的应用中有很大的不同。
首先,我试试你的代码找不到IBinder,bindService,我试试改成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);
}
});
但是在class Zing extends Services 中我无法获取Extra(values)。因为这个类扩展了服务。
谢谢。
【问题讨论】:
-
IBinder 在 API 23 中:developer.android.com/intl/es/reference/android/os/IBinder.html
标签: java android configuration