Modification label bouton via préférence
Bonjour,
mon application est une sorte de placard à bouton qui lance des commandes via socket IP
L'utilisateur peut via le menu option et les préference choisir l'adresse ip du socket et cela marche tres bien
je souhaite maintenant donner la possibilité à l'utilisateur de modifier le nom des boutons
j'ai donc ajouté deux préferences bouton1_name et bouton2_name
ligne 37
je n'arrive pas a recuperer la valeur de la préference, j'ai toujours la valeur par defaut qui s'affiche
seconde question: je ne sais pas ou mettre ce code pour que les label soient correct au demarrage de l'app et en sortant des preferences??
main
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
package com.example.planar;
import java.io.*;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
//private String ipPcControl ="192.168.1.11";
private String port7001PcControl ="7001";
private String portSynlinkPcControl ="7072";
//private String portPliPcControl ="7001";
private String imageDelete ="image_delete -1 gui\n\n";
private String loadConfig1 ="send_message -dest indisys_director -msg {load_config preset_1}";
private String loadConfig2 ="send_message -dest indisys_director -msg {load_config preset_2}";
private String loadConfig3 ="send_message -dest indisys_director -msg {load_config preset_3}";
private String loadConfig4 ="send_message -dest indisys_director -msg {load_config preset_4}";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// write preference buton1 name
SharedPreferences mgr = PreferenceManager.getDefaultSharedPreferences(this);
String buton_name = mgr.getString("buton1_name", "defaut");
Toast.makeText(this, buton_name, Toast.LENGTH_LONG).show();
Button button = (Button)findViewById(R.id.button_without_listener);
button.setText(buton_name);
// end write preference buton1 name
}
public void onResume() {
super.onResume();
}
// MENU OPTION
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.about:
startActivity(new Intent(this, About.class));
return true;
case R.id.settings:
startActivity(new Intent(this, Settings.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
// button
/** Name of the method and it's triggering comes from main.xml file.
* Button's widget attribute android:onClick allows you to specify name of the method
* which you have to implement.
* @throws IOException **/
public void onButtonClick(View v) throws IOException {
SharedPreferences mgr = PreferenceManager.getDefaultSharedPreferences(this);
String ipPcControl = mgr.getString("ip", "defaut");
Toast.makeText(this, ipPcControl, Toast.LENGTH_LONG).show();
new ClientSocket().execute(ipPcControl,portSynlinkPcControl,imageDelete);
}
public void onButtonClick2(View v) throws IOException {
SharedPreferences mgr = PreferenceManager.getDefaultSharedPreferences(this);
String ipPcControl = mgr.getString("ip", "defaut");
new ClientSocket().execute(ipPcControl,port7001PcControl,loadConfig1);
}
public void onButtonClick3(View v) throws IOException {
SharedPreferences mgr = PreferenceManager.getDefaultSharedPreferences(this);
String ipPcControl = mgr.getString("ip", "defaut");
new ClientSocket().execute(ipPcControl,port7001PcControl,loadConfig2);
}
public void onButtonClick4(View v) throws IOException {
SharedPreferences mgr = PreferenceManager.getDefaultSharedPreferences(this);
String ipPcControl = mgr.getString("ip", "defaut");
new ClientSocket().execute(ipPcControl,port7001PcControl,loadConfig3);
}
public void onButtonClick5(View v) throws IOException {
SharedPreferences mgr = PreferenceManager.getDefaultSharedPreferences(this);
String ipPcControl = mgr.getString("ip", "defaut");
new ClientSocket().execute(ipPcControl,port7001PcControl,loadConfig4);
}
} |
preference
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceScreen android:title="Parametres" >
<PreferenceCategory android:title="Définir IP PC controle" >
<EditTextPreference android:title="Ip du controle" android:key="ip">
</EditTextPreference>
</PreferenceCategory>
<PreferenceCategory android:title="Nom des boutons" >
<EditTextPreference android:title="Bouton 1" android:key="buton1_name">
</EditTextPreference>
<EditTextPreference android:title="Bouton 2" android:key="buton2_name">
</EditTextPreference>
</PreferenceCategory>
</PreferenceScreen>
</PreferenceScreen> |