将首选项屏幕添加到具有自定义窗口标题的活动

发布于 2024-12-10 00:57:43 字数 2440 浏览 1 评论 0原文

我知道可以向首选项屏幕活动添加按钮。我读了这个问题 如何向 PreferenceScreen 添加按钮 它工作完美。 但如果我想添加自定义标题,我的应用程序就会崩溃。 自定义标题在其自己的活动中完美运行,带有按钮的首选项屏幕在其自己的活动中也完美运行。所以我的问题只是基于将这两个功能结合在一起。

这是我的 java 文件中的代码,

public class TestHeadlineActivity extends PreferenceActivity implements OnClickListener{
   /** Called when the activity is first created. */
Button settingsButton;
TextView tv_WindowTitleAccuracy;

    @Override
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
       addPreferencesFromResource(R.xml.prefs);
       setContentView(R.layout.main);
       getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
       settingsButton = (Button) findViewById(R.id.Button_Settings);
       settingsButton.setText("BUTTONTEXT");
   settingsButton.setOnClickListener(this);
   tv_WindowTitleAccuracy = (TextView) findViewById(R.id.tv_WindowTitleAccuracy);
   tv_WindowTitleAccuracy.setText("TEXT");

   }

 @Override
 public void onClick(View v) {

     finish();

       }
      }

这是我的主要 xml

<?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="fill_parent"
           android:orientation="vertical">
<ListView android:id="@android:id/list"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent" android:layout_weight="10"/>
<Button android:text="This is a button on top of all preferences."
          android:layout_height="wrap_content" 
          android:layout_weight="1" android:layout_width="fill_parent"/>
</LinearLayout>

,这是我的首选项,

 <?xml version="1.0" encoding="utf-8"?>
 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

 <EditTextPreference
    android:title="EditText"
    android:key="name"
    android:summary="Enter your name"
    ></EditTextPreference>

  <CheckBoxPreference 
    android:title="Music"
    android:defaultValue="true"
    android:key="checkbox"
    android:summary="for the splash screen"
     />
  </PreferenceScreen>

谢谢大家

I know its possible to add a button to a prefernces Screeen activtiy. I read this question
How to add a button to PreferenceScreen
and it works perfect.
But if I want to add my custom title my application just crashes.
The customtitle works perfect in its own activity and the preference screen with button works also perfect in its own activity. So my problem is only based on bringing these two functions together.

heres my code in of the java file

public class TestHeadlineActivity extends PreferenceActivity implements OnClickListener{
   /** Called when the activity is first created. */
Button settingsButton;
TextView tv_WindowTitleAccuracy;

    @Override
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
       addPreferencesFromResource(R.xml.prefs);
       setContentView(R.layout.main);
       getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
       settingsButton = (Button) findViewById(R.id.Button_Settings);
       settingsButton.setText("BUTTONTEXT");
   settingsButton.setOnClickListener(this);
   tv_WindowTitleAccuracy = (TextView) findViewById(R.id.tv_WindowTitleAccuracy);
   tv_WindowTitleAccuracy.setText("TEXT");

   }

 @Override
 public void onClick(View v) {

     finish();

       }
      }

this is my main xml

<?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="fill_parent"
           android:orientation="vertical">
<ListView android:id="@android:id/list"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent" android:layout_weight="10"/>
<Button android:text="This is a button on top of all preferences."
          android:layout_height="wrap_content" 
          android:layout_weight="1" android:layout_width="fill_parent"/>
</LinearLayout>

and heres my prefs

 <?xml version="1.0" encoding="utf-8"?>
 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

 <EditTextPreference
    android:title="EditText"
    android:key="name"
    android:summary="Enter your name"
    ></EditTextPreference>

  <CheckBoxPreference 
    android:title="Music"
    android:defaultValue="true"
    android:key="checkbox"
    android:summary="for the splash screen"
     />
  </PreferenceScreen>

thanks guys

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空‖城人不在 2024-12-17 00:57:43

仍然没有答案?!也许你已经解决了它,但也许其他人会发现其中一些链接有用:

干杯

still no answer?! probably u solved it already but maybe somebody else will find some of this links useful:

cheers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文