我是Android的新手.我正在学习PreferenceActivity.
我需要一些关于如何在它们右侧显示EditTextPreference的当前值的指导.像这样:
--------------------------------------
|EditTextPreference value |
|"Summary" |
--------------------------------------
这可能是代码:
activity_main.xml中
setting_preference.xml
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intentSetting = new Intent(MainActivity.this,Setting.class);
startActivityForResult(intentSetting,1);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main,menu);
return true;
}
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data) {
super.onActivityResult(requestCode,resultCode,data);
int k = 0;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button,so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
我真的很感激你的帮助!
最佳答案
您需要为此创建自定义视图.
原文链接:https://www.f2er.com/android/431202.htmlpublic class EditTextPreferenceWithValue extends EditTextPreference {
private TextView textValue;
public EditTextPreferenceWithValue(Context context) {
super(context);
setLayoutResource(R.layout.preference_with_value);
}
public EditTextPreferenceWithValue(Context context,AttributeSet attrs) {
super(context,attrs);
setLayoutResource(R.layout.preference_with_value);
}
public EditTextPreferenceWithValue(Context context,AttributeSet attrs,int defStyle) {
super(context,attrs,defStyle);
setLayoutResource(R.layout.preference_with_value);
}
@Override
protected void onBindView(View view) {
super.onBindView(view);
textValue = (TextView) view.findViewById(R.id.preference_value);
if (textValue != null) {
textValue.setText(getText());
}
}
@Override
public void setText(String text) {
super.setText(text);
if (textValue != null) {
textValue.setText(getText());
}
}
}
preference_with_value.xml