2015-07-02 23 views
0

我正在製作一個應用程序,您可以製作和刪除標籤。 我想製作2個按鈕,您可以創建一個選項卡並刪除選項卡。 我希望按鈕與設置位於同一活動中,但似乎不起作用。延伸創建按鈕PreferenceActivity

這裏是我的AppPreferences:

public class AppPreferences extends PreferenceActivity { 
@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    addPreferencesFromResource(R.xml.preferences); 

} 
public void createtab(View view){ 

    Toast.makeText(AppPreferences.this, "Button 1", Toast.LENGTH_SHORT).show(); 

} 
public void removetab(View view){ 

    Toast.makeText(AppPreferences.this, "Button 2", Toast.LENGTH_SHORT).show(); 

} 

,這是我的佈局:

<Button 
    style="?android:attr/buttonStyleSmall" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Create Tab" 
    android:id="@+id/createtab" 
    android:onClick="createtab" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    /> 

<Button 
    style="?android:attr/buttonStyleSmall" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Remove Tab" 
    android:id="@+id/removetab" 
    android:onClick="removetab" 
    android:layout_alignBottom="@+id/createtab" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true" 
    /> 

感謝您的閱讀,我希望你能幫幫我!

+0

是否要對bu執行這些操作tton點擊? –

回答

0

How to add a button to a PreferenceScreen (Android), 你可以像這樣做:

<PreferenceScreen android:title="@string/login" android:key="Login"> 
    <EditTextPreference android:persistent="true" android:title="@string/username" android:key="Username"></EditTextPreference> 
    <EditTextPreference android:title="@string/password" android:persistent="true" android:password="true" android:key="Password"></EditTextPreference> 
    <Preference android:layout="@layout/loginButtons" android:key="loginButtons"></Preference> 

和佈局文件(layoutButtons.xml)看起來是這樣:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:weightSum="10" 
    android:baselineAligned="false" android:orientation="horizontal"> 
    <Button android:text="Login" android:layout_width="fill_parent" 
     android:layout_weight="5" android:layout_height="wrap_content" 
     android:id="@+id/loginButton" android:layout_gravity="left"></Button> 
    <Button android:text="Password?" android:layout_width="fill_parent" 
     android:layout_weight="5" android:layout_height="wrap_content" 
     android:id="@+id/forgottenPasswordButton"></Button> 
</LinearLayout> 

你可以添加OnClick ..