添加一个按钮到prefrenceScreen:安卓 [英] Add a Button into PrefrenceScreen : Android
问题描述
你好我想添加一个按钮到prefrencescreen,我得到了成功进入一个按钮添加到prefrence,但我无法得到onclick事件。我重视prefence屏幕我的codeA PIC
Hi i want to add a button into prefrencescreen, i got success into add a button into the prefrence but i could not get onClick event. i have attached my code a pic of prefence screen
Setting.xml的
setting.xml
<PreferenceCategory android:title="Application Details">
<Preference android:key="type"
android:title="Type"
android:summary="" />
</PreferenceCategory>
<PreferenceCategory android:title="Notification Settings">
<ListPreference android:key="sendNotificationType"
android:title="Status Notification For"
android:dialogTitle="Status Notification For" />
</PreferenceCategory>
settingdetail.xml
settingdetail.xml
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/textView2"
android:layout_marginLeft="15dp"
android:text="Type"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginLeft="15dp"
android:layout_toLeftOf="@+id/setFromTimeBtn"
android:text="Summary"
android:textAppearance="?android:attr/textAppearanceSmall" />
<Button
android:id="@+id/buyItNowBtn"
android:layout_width="80dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_marginRight="15dp"
android:layout_centerVertical="true"
android:background="@drawable/button"
android:text="@string/buyItNowBtnTxt"
android:textColor="@color/white" />
和prefenceActivity类的onCreate方法
and the onCreate Method of prefenceActivity Class
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.layout.setting);
setContentView(R.layout.settingview);
Preference typePref = (Preference) findPreference("type");
typePref.setLayoutResource(R.layout.settingdetail);
typePref.setSelectable(true);
Button btn = (Button) findViewById(R.id.buyItNowBtn);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
Log.e(TAG,"TEST");
Toast.makeText(Setting.this, "TEST", Toast.LENGTH_SHORT).show();
}
});
}
截图
推荐答案
这线程是过时的,但由于没有解决办法呢,这是我如何成功的:
This thread is stale, but since there is no solution yet, here is how I succeeded:
1。设置按钮,点击监听器
有两种方法来设置点击监听器的按钮,以编程方式或通过XML。
There are two ways to set the click listener for a button, either programmatically or via XML.
编程方式:的在您的自定义preference,覆盖 onBindView
并附加监听器有:
Programmatically: In your custom preference, override onBindView
and attach the listener there:
@Override
protected void onBindView(View view) {
View button = view.findViewById(R.id.myButton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d(LOG_TAG, "button click listener works!");
}
});
super.onBindView(view);
}
通过XML:的添加安卓的onClick =yourMethodName
到按钮的布局定义
元素(这是你的preference布局的一部分,见下文),并在实施该方法的 preferenceActivity
如下所述:< A HREF =http://developer.android.com/guide/topics/ui/controls/button.html#HandlingEvents相对=nofollow>响应点击活动
Via XML: Add android:onClick="yourMethodName"
to the layout definition of the Button
element (which is part of the layout for your preference, see below) and implement the method in your PreferenceActivity
as described here: Responding to Click Events
2。固定在preferenceClickListener
现在的按钮,点击监听器应该工作,但preference的在preferenceClickListener
将不再工作。为了解决这个问题,添加
Now the button click listener should work, but the Preference's OnPreferenceClickListener
will not work anymore. In order to fix this, add
android:descendantFocusability="blocksDescendants"
在你的preference布局文件顶部的元素:
to the top element in the layout file for your preference:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:paddingEnd="?android:attr/scrollbarSize"
android:background="?android:attr/selectableItemBackground"
android:descendantFocusability="blocksDescendants">
<ImageView
android:id="@android:id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dip"
android:layout_marginEnd="6dip"
android:layout_marginTop="6dip"
android:layout_marginBottom="6dip"
android:layout_weight="1">
<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" />
<TextView android:id="@android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@android:id/title"
android:layout_alignStart="@android:id/title"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="?android:attr/textColorSecondary"
android:maxLines="4" />
<Button android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
</RelativeLayout>
<!-- Preference should place its actual preference widget here. -->
<LinearLayout android:id="@android:id/widget_frame"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical" />
</LinearLayout>
(否机器人:的onClick =yourMethodName
这里我选择了以编程方式做到这一点)
(No android:onClick="yourMethodName"
here as I chose to do it programmatically.)
3。安装布局
最后,你需要这样的布局设置为您的preference XML文件:
添加机器人:布局=@布局/ myLayoutName
或可选择地通过 setLayoutResource
将其设置为你构建preference在 preferenceFragment
。
Finally you need to set this layout to your preference XML file:
Add android:layout="@layout/myLayoutName"
or alternatively set it via setLayoutResource
as you construct the preference in your PreferenceFragment
.
这篇关于添加一个按钮到prefrenceScreen:安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!