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

时间:2015-01-19 07:56:03

标签: android

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

-------------------------------------- 
|EditTextPreference            value |
|"Summary"                           |
--------------------------------------

这可能是代码:

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.prefereceproject.MainActivity" >

    <Button 
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Dialog"/>
</RelativeLayout>

setting_preference.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <EditTextPreference 
        android:title="Your Age"
        android:key="yourAge"
        android:summary="Please provide your age"
        android:inputType="numberDecimal">
    </EditTextPreference>
</PreferenceScreen>

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);
    }
}

我真的很感激你的帮助!

2 个答案:

答案 0 :(得分:3)

您需要为此创建自定义视图。

public 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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:gravity="center_vertical"
    android:paddingRight="?android:attr/scrollbarSize">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dip"
        android:layout_marginRight="6dip"
        android:layout_marginTop="6dip"
        android:layout_marginBottom="6dip"
        android:layout_weight="1">

        <LinearLayout
            android:id="@+id/preference_first_line"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical">

            <TextView android:id="@+android:id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="15dip"
                android:layout_weight="1"
                android:singleLine="true"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:ellipsize="marquee"
                android:fadingEdge="horizontal" />

            <TextView android:id="@+id/preference_value"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="10"
                android:singleLine="true"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:gravity="right"
                android:ellipsize="end" />

        </LinearLayout>

        <TextView android:id="@+android:id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/preference_first_line"
            android:layout_alignLeft="@android:id/title"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:maxLines="2" />

    </RelativeLayout>

</LinearLayout>

答案 1 :(得分:1)

很棒的帖子!我有点像这样“优化”了你的xml,有点简单。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="6dip"
            android:layout_marginLeft="15dip"
            android:layout_marginRight="6dip"
            android:layout_marginTop="6dip"
            android:gravity="center_vertical"
            android:minHeight="?android:attr/listPreferredItemHeight"
            android:paddingRight="?android:attr/scrollbarSize">


<TextView
    android:id="@+android:id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="15dip"
    android:layout_weight="1"
    android:ellipsize="marquee"
    android:fadingEdge="horizontal"
    android:hint="title"
    android:singleLine="true"
    android:textAppearance="?android:attr/textAppearanceLarge"/>

<TextView
    android:id="@+id/preference_value"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_weight="10"
    android:ellipsize="end"
    android:gravity="right"
    android:hint="value"
    android:singleLine="true"
    android:textAppearance="?android:attr/textAppearanceSmall"/>


<TextView
    android:id="@android:id/summary"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@android:id/title"
    android:layout_below="@android:id/title"
    android:hint="summary"
    android:maxLines="2"
    android:textAppearance="?android:attr/textAppearanceSmall"/>