在preference活动无法点击按钮集 [英] Unclickable button set on a preference activity

查看:235
本文介绍了在preference活动无法点击按钮集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只设置一个preference活动按钮,从一个从这个<一个答案href=\"http://stackoverflow.com/questions/2697233/how-to-add-a-button-to-$p$pferencescreen\">question.我的主要问题是,这个按钮是不可点击,而preference活动的其他元素是可以点击

我创建了一个简单的例子来证明我的困境,应该表现出相同的症状,你复制和粘贴。

 公共类preferenceTest扩展$ P $ {pferenceActivity私人Button按钮;
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    // TODO自动生成方法存根
    super.onCreate(savedInstanceState);    的setContentView(R.layout.activity_main);
    。getFragmentManager()调用BeginTransaction()取代(android.R.id.content,新的我的preferenceFragment())提交()。
    按钮=(按钮)findViewById(R.id.button);    button.setOnClickListener(新OnClickListener(){        @覆盖
        公共无效的onClick(查看为arg0){
            // TODO自动生成方法存根
            Toast.makeText(getApplicationContext(),我得到了点击Toast.LENGTH_SHORT).show();
        }
    });}
 公共静态类我的preferenceFragment扩展preferenceFragment
    {        @覆盖
        公共无效的onCreate(最终捆绑savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            加preferencesFromResource(R.xml preftest);
            //做的东西
        }
    }
}

RES \\布局\\ activity_main.xml中

 &LT;?XML版本=1.0编码=UTF-8&GT?;
&LT; LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
          机器人:layout_width =FILL_PARENT
          机器人:layout_height =FILL_PARENT
          机器人:方向=垂直&GT;&LT; ListView的机器人:ID =@机器人:ID /列表
          机器人:layout_width =FILL_PARENT
          机器人:layout_height =FILL_PARENT
          机器人:文字颜色=@机器人:彩色/黑白
          机器人:layout_weight =10/&GT;&LT;按钮的android:文本=这是所有preferences的底部有一个按钮
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:ID =@ + ID /按钮
        机器人:文字颜色=@机器人:彩色/黑白
        机器人:layout_weight =1/&GT;
&LT; / LinearLayout中&GT;

RES \\ XML \\ preftest.xml
    
    

 &LT; preferenceCategory
    机器人:标题=新标题
    机器人:总结=标题&GT;    &LT;名单preference
        机器人:键=list1的
        机器人:标题=列出一个
        机器人:总结=的List1
        /&GT;    &LT;名单preference
        机器人:键=列表2
        机器人:标题=列出两个
        机器人:总结=列表2/&GT;&LT; / preferenceCategory&GT;&LT;复选框preference
    机器人:键=CHECK1
    机器人:标题=CHECK1
    机器人:总结=复选框测试/&GT;&LT; / preferenceScreen&GT;


解决方案

我不知道你为什么会想在一个preferenceActivity添加一个按钮,但你可以做的就是添加一个简单的 preference ,然后让通过活动code的点击。

例如,在你的 preferences.xml

 &LT; preference
        机器人:键=@字符串/ pref_key_dummy_ pref_button
        机器人:标题=@字符串/ pref_title_dummy_ pref_button/&GT;

然后,在你的preferenceActivity,创建一个preference对象:

  preference mDummyButton preF;

从的onCreate初始化:

 添加preferencesFromResource(R.xml preferences);
mDummyButton preF =找到preference(的getString(R.string pref_key_dummy_ pref_button));

然后,在preferenceTreeClicked添加覆盖处理点击:

  @覆盖
@德precated
公共布尔在preferenceTreeClick(preferenceScreen preferenceScreen,
        preference preference){
    如果(preference == mDummyButton preF){
        Log.v(TAG,虚拟得到点击);
    }
}

I just set a button on a preference activity from one the answers from this question. My main issue is that this button is unclickable while the other elements of the preference activity is click able.

I created a simple example to demonstrate my dilemma it should show the same symptoms as you copy and paste.

public class preferenceTest extends PreferenceActivity{

private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
    button = (Button)findViewById(R.id.button);

    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "I got clicked", Toast.LENGTH_SHORT).show();
        }
    });

}
 public static class MyPreferenceFragment extends PreferenceFragment
    {

        @Override
        public void onCreate(final Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.preftest);
            // Do Stuff
        }
    }


}

res\layout\activity_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:textColor="@android:color/black"
          android:layout_weight="10"/>

<Button android:text="This is a button on bottom of all preferences."
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button" 
        android:textColor="@android:color/black"
        android:layout_weight="1"/>
</LinearLayout>

res\xml\preftest.xml

<PreferenceCategory
    android:title="New Title"
    android:summary="Title">

    <ListPreference 
        android:key="list1"
        android:title="List one"
        android:summary="List1"
        />

    <ListPreference 
        android:key="list2"
        android:title="List two"
        android:summary="List2"/>

</PreferenceCategory>

<CheckBoxPreference
    android:key="check1"
    android:title="check1"
    android:summary="CheckBox Test"/>

</PreferenceScreen>

解决方案

I'm not sure why you would want to add a button in a PreferenceActivity, but what you can do is add a plain Preference and then make that clickable through the activity code.

For instance, on your preferences.xml

<Preference
        android:key="@string/pref_key_dummy_pref_button"
        android:title="@string/pref_title_dummy_pref_button" />

Then, on your PreferenceActivity, create a Preference object:

Preference mDummyButtonPref;

Initialize it from onCreate:

addPreferencesFromResource(R.xml.preferences);
mDummyButtonPref= findPreference(getString(R.string.pref_key_dummy_pref_button));

Then, add override onPreferenceTreeClicked to handle clicks:

@Override
@Deprecated
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
        Preference preference) {
    if (preference == mDummyButtonPref) {
        Log.v(TAG, "Dummy got clicked");
    }
}

这篇关于在preference活动无法点击按钮集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆