android – 如何在EditTextPreference的右侧显示当前值?

前端之家收集整理的这篇文章主要介绍了android – 如何在EditTextPreference的右侧显示当前值?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我是Android的新手.我正在学习PreferenceActivity.
我需要一些关于如何在它们右侧显示EditTextPreference的当前值的指导.像这样:

  1. --------------------------------------
  2. |EditTextPreference value |
  3. |"Summary" |
  4. --------------------------------------

这可能是代码

activity_main.xml中

setting_preference.xml

MainActivity.java

  1. public class MainActivity extends Activity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. Button btn = (Button) findViewById(R.id.btn);
  7. btn.setOnClickListener(new OnClickListener() {
  8. @Override
  9. public void onClick(View v) {
  10. // TODO Auto-generated method stub
  11. Intent intentSetting = new Intent(MainActivity.this,Setting.class);
  12. startActivityForResult(intentSetting,1);
  13. }
  14. });
  15. }
  16. @Override
  17. public boolean onCreateOptionsMenu(Menu menu) {
  18. // Inflate the menu; this adds items to the action bar if it is present.
  19. getMenuInflater().inflate(R.menu.main,menu);
  20. return true;
  21. }
  22. @Override
  23. protected void onActivityResult(int requestCode,int resultCode,Intent data) {
  24. super.onActivityResult(requestCode,resultCode,data);
  25. int k = 0;
  26. }
  27. @Override
  28. public boolean onOptionsItemSelected(MenuItem item) {
  29. // Handle action bar item clicks here. The action bar will
  30. // automatically handle clicks on the Home/Up button,so long
  31. // as you specify a parent activity in AndroidManifest.xml.
  32. int id = item.getItemId();
  33. if (id == R.id.action_settings) {
  34. return true;
  35. }
  36. return super.onOptionsItemSelected(item);
  37. }
  38. }

我真的很感激你的帮助!

最佳答案
您需要为此创建自定义视图.

  1. public class EditTextPreferenceWithValue extends EditTextPreference {
  2. private TextView textValue;
  3. public EditTextPreferenceWithValue(Context context) {
  4. super(context);
  5. setLayoutResource(R.layout.preference_with_value);
  6. }
  7. public EditTextPreferenceWithValue(Context context,AttributeSet attrs) {
  8. super(context,attrs);
  9. setLayoutResource(R.layout.preference_with_value);
  10. }
  11. public EditTextPreferenceWithValue(Context context,AttributeSet attrs,int defStyle) {
  12. super(context,attrs,defStyle);
  13. setLayoutResource(R.layout.preference_with_value);
  14. }
  15. @Override
  16. protected void onBindView(View view) {
  17. super.onBindView(view);
  18. textValue = (TextView) view.findViewById(R.id.preference_value);
  19. if (textValue != null) {
  20. textValue.setText(getText());
  21. }
  22. }
  23. @Override
  24. public void setText(String text) {
  25. super.setText(text);
  26. if (textValue != null) {
  27. textValue.setText(getText());
  28. }
  29. }
  30. }

preference_with_value.xml

猜你在找的Android相关文章