2016-04-05 14 views
0

私はユーザの入力時にEditTextserverStringである設定レイアウトを持っています。Config Layout AndroidでEditTextからテキストを取得するには?

<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は柔軟ではありません。だから私は、ユーザーが入力したテキストをConfigurationに保存すると、serverUrlSTringこのテキストを取得してプロセスを続行します。

更新日:

を@EduardoヤニェスPararedaへ送る:

私はあなたの更新されたコードを試してみました。それは私のアプリケーションでは非常に異なっています。 まず、私はbindService、それがIBinderを見つけることができないあなたのコードを試してみてください、私は好きに変更してみてください。

serverUrl.setOnClickListener(新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することはできません(値)。このクラスは、サービスを拡張しているため。

感謝。

+0

IBinderは、API 23である。http://developer.android.com/intl/es /reference/android/os/IBinder.html –

答えて

1

あなたが得る、その後、テントを使用してサービスを開始し、余分な値を送信する、方法ONSTARTをオーバーライドする場合意図と関連情報:

//それとも、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; 
    } 
} 
+0

私は知っています。私はこの自動保存をしたい。しかし、クラス 'Zing.java'でこの値を取得する方法は、Servicesを拡張します。 – vanloc

+0

回答更新... –

+0

これは動作していないようです。私は私の質問を更新しました。 – vanloc

1
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(); 
     } 
    }); 
関連する問題