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

5

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

我真的非常感激你的帮助!


设置布局重力为右侧。 - Piyush
你能写得更详细一些吗?谢谢! - Trinh DL
你正在使用哪种布局来设置EditTextPreference? - Piyush
3个回答

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>

textValue = (TextView) view.findViewById(R.id.pref_value)。 pref_value 是在哪里添加的?@MurtazaHussain - Trinh DL
感谢@MurtazaHussain。我会尝试去做。 - Trinh DL

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"/>


我知道这是一个打字错误,但你缺少了一个闭合标签 </RelativeLayout> - James Poulose

0

使用自定义偏好小部件可能更简单,避免与其他偏好设置样式不一致。

public class ListPreferenceX extends ListPreference {
    private TextView textCurValue;

    public ListPreferenceX(Context context) {
        super(context);
        setWidgetLayoutResource(R.layout.preference_with_value);
    }
    public ListPreferenceX(Context context, AttributeSet attrs) {
        super(context, attrs);
        setWidgetLayoutResource(R.layout.preference_with_value);
    }

    public ListPreferenceX(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setWidgetLayoutResource(R.layout.preference_with_value);
    }

    @Override
    protected void onBindView(View view) {
        super.onBindView(view);
        textCurValue = view.findViewById(R.id.preference_value);
        if (textCurValue != null) {
            textCurValue.setText(getEntry());
        }
    }

}

还有 preference_with_value.xml 文件。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/widget_frame"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:gravity="center_vertical">

    <TextView android:id="@+id/preference_value"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"/>

</LinearLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接