【问题标题】:In Android Studio why is there no TextViewPreference?在 Android Studio 中,为什么没有 TextViewPreference?
【发布时间】:2020-07-10 07:43:11
【问题描述】:

好的,所以我想创建一个设置活动。有人告诉我使用 Preference 布局会更容易。 我想要一个 TextView,当单击它时会执行某个操作,但没有 TextViewPreference。最接近的是 EditTextPreference,它必须将属性“可选择”设置为关闭,这会将其变为灰色。

另外,这些首选项没有 ID 属性。如果他们没有 ID,我应该如何将 OnClickListener 附加到他们???

【问题讨论】:

  • 你到底想要什么功能......?你的问题不清楚......你有没有解决@Doc给出的链接?
  • @Wini 我只是希望能够在 Preference Screen 布局中放置一个 TextView

标签: java android android-studio


【解决方案1】:

尝试-->

方法一

创建一个自定义首选项布局项并在 Preference Activity 中使用它:-

<?xml version="1.0" encoding="utf-8"?>
<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">

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

  </LinearLayout>

添加偏好xml:

<Preference
android:title="@string/label_pref_version"
android:key="@string/pref_version"
android:layout="@layout/pref" />

最后:-

在您的 Activity 代码中触发 findViewById 并附加一个侦听器....

方法二

对于 xml:

<Preference android:title="Acts like a text"
            android:key="@string/myCooltext"
            android:summary="This is a text"/>

那么对于你的 onCreate() 中的 java

Preference text = findPreference(getString(R.string.myCoolText));
text.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {   
                //code for what you want it to do   
                return true;
            }
        });

方法三

您可以这样尝试让 Textview 处于活动状态:

 View name= ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.layouttext, null, false);

将android:id添加到layouttext.xml中包含TextviewLinearLayout,即android:id="@+id/mylayout"

 LinearLayout mLayout = (LinearLayout)name.findViewById(R.id.mylayout);
  Textview login = (Textview) footerView.findViewById(R.id.Text);
  login.setOnClickListener(this);

【讨论】:

  • 嗨。使用方法 2 我找不到 findPreference() 方法
  • 你在扩展什么? PreferenceFragmentPreferenceActivity ?????\
  • 第二个。顺便说一句,公共布尔 onPreferenceClick(Preference 首选项) 中的布尔值是什么?
  • 在点击首选项时调用.....如果点击被处理则为真.....
  • 如果您不处理点击,即点击时您不执行任何操作 --> 返回 false
猜你喜欢
  • 1970-01-01
  • 2022-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多