【问题标题】:How to get text from EditText in Config Layout Android?如何从 Config Layout Android 中的 EditText 获取文本?
【发布时间】:2016-07-25 00:42:31
【问题描述】:

当用户输入时,我的配置布局包含 EditTextserverString

<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:

我尝试了您更新的代码。它在我的应用中有很大的不同。 首先,我试试你的代码找不到IBinderbindService,我试试改成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)。因为这个类扩展了服务。

谢谢。

【问题讨论】:

标签: java android configuration


【解决方案1】:

如果你想用一个 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;
    }
}

【讨论】:

【解决方案2】:
 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();
        }
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-21
    • 2014-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多