2012-04-04 26 views
10

Tôi có một thiết lập Preferences làm việc được khởi chạy từ một tùy chọn trình đơn. Trong các sở thích tôi đã thiết lập một sở thích tùy chỉnh mà phải khởi động một hộp thoại với 3 TextViews để thiết lập xác nhận và thay đổi mật khẩu. Bây giờ tôi không biết cách khởi chạy hộp thoại từ onference Preference của OnferenceActivity. Nếu tôi âm thanh như một người mới - tôi, xin lỗi!Làm thế nào để thực hiện một PreferenceActivity khởi động một Dialog để thiết lập một sở thích tùy chỉnh

Đây là bố cục xml của tôi cho popup thoại:

<?xml version="1.0" encoding="UTF-8"?> 
<LinearLayout 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    android:orientation="vertical" 
    android:id="@+id/root" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

    <TextView 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:id="@+id/TextView_Pwd1" 
     android:text="@string/settings_oldpassword" 
     android:textStyle="bold" /> 

    <EditText 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:id="@+id/EditText_OldPwd" /> 

    <TextView 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:id="@+id/TextView_Pwd1" 
     android:text="@string/settings_password" 
     android:textStyle="bold" /> 

    <EditText 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:id="@+id/EditText_Pwd1" 
     android:inputType="textPassword" /> 

    <TextView 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:id="@+id/TextView_Pwd2" 
     android:text="@string/settings_password2" 
     android:textStyle="bold" /> 

    <EditText 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:id="@+id/EditText_Pwd2" 
     android:inputType="textPassword" /> 

    <TextView 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:id="@+id/TextView_PwdProblem" 
     android:textStyle="bold" 
     android:gravity="center" /> 

    <TextView 
     android:id="@+id/TextView_PwdProblem" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:text="@string/settings_pwd_not_equal" /> 

    <CheckBox 
     android:id="@+id/checkShowPwdText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/settings_showpwd_text" /> 

Đây là lớp DialogChangePassword tôi cho popup thoại:

package biz.linsys.package; 

import android.app.Dialog; 
import android.content.Context; 
import android.content.SharedPreferences; 
import android.preference.DialogPreference; 
import android.text.Editable; 
import android.text.TextWatcher; 
import android.util.AttributeSet; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.TextView; 

public class DialogChangePassword extends DialogPreference { 

    private String strPass1; 
    private String strPass2; 

    public DialogChangePassword(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     setDialogLayoutResource(R.layout.dialog_pwdchange); 
    } 

    @Override 
    protected void onBindDialogView(View view) { 

     Dialog pwdDialog   = getDialog(); 
     final EditText password1 = (EditText) pwdDialog.findViewById(R.id.EditText_Pwd1); 
     final EditText password2 = (EditText) pwdDialog.findViewById(R.id.EditText_Pwd2); 
     final TextView error  = (TextView) pwdDialog.findViewById(R.id.TextView_PwdProblem);  

     password2.addTextChangedListener(new TextWatcher() { 

      @Override 
      public void afterTextChanged(Editable s) { 

       strPass1 = password1.getText().toString(); 
       strPass2 = password2.getText().toString(); 

       if (strPass1.equals(strPass2)) { 

        error.setText(R.string.settings_pwd_equal); 
       } else { 

        error.setText(R.string.settings_pwd_not_equal); 
       } 
      } public void beforeTextChanged(CharSequence s, int start, int count, int after) {} 
       public void onTextChanged(CharSequence s, int start, int before, int count) {} 
     }); 

     super.onBindDialogView(view); 

    } 

    @Override 
    protected void onDialogClosed(boolean positiveResult) { 

     if(!positiveResult) return; 

     SharedPreferences.Editor editor = getEditor(); 

     if (strPass1.equals(strPass2)) { 

      editor.putString("password", strPass1); 
      editor.commit(); 
     } 

     super.onDialogClosed(positiveResult); 

    } 
} 

này lớp PreferenceActivity chứa Tuỳ chỉnh Preference onPreferenceClick. Đây là nơi tôi cần gọi hộp thoại để thay đổi cài đặt mật khẩu người dùng.

package biz.linsys.package; 

import android.content.Context; 
import android.os.Bundle; 
import android.preference.Preference; 
import android.preference.Preference.OnPreferenceClickListener; 
import android.preference.PreferenceActivity; 

public class Preferences extends PreferenceActivity { 

    public static Context dialogContext; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

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

     // Get the custom preference 
     Preference customPref = (Preference) findPreference("customPref"); 

     customPref.setOnPreferenceClickListener(new OnPreferenceClickListener() { 

      public boolean onPreferenceClick(Preference preference) { 

       // [ NEED TO CALL DIALOG FROM HERE ] 

       return false; 
      } 
     }); 
    } 
} 
+0

Vì vậy, bạn có hoạt động tùy chọn, khi nhấp vào khởi chạy hộp thoại có nút? –

+0

@MasterJB: Có, hộp thoại có các nút. Tôi nhận ra rằng tôi không bao gồm các nút trong tệp XML Layout. – Lukuluba

+0

Tôi cũng hỏi rằng, trong quá khứ, tôi đã khởi chạy một hoạt động nhỏ từ một nút có giao diện đối thoại được thiết lập từ bên trong tệp kê khai: "android: theme =" @ android: style/Theme.Dialog ". Bạn sẽ làm điều này bằng cách chỉ đơn giản là nhằm vào hoạt động và wit sẽ bật lên trong một đối thoại. –

Trả lời

13

Đây là thứ thiếu trong tài liệu và tôi đã tìm thấy rất nhiều câu hỏi tương tự liên quan đến nó, hầu hết không có câu trả lời rõ ràng. Tôi đã phải đối mặt với cùng một vấn đề ngày hôm nay, và bằng cách nào đó tôi đã tìm thấy giải pháp, vì vậy tôi sẽ tóm tắt nhiệm vụ của tôi ở đây, chỉ hy vọng một người nào đó sẽ thấy điều này hữu ích. BTW câu hỏi của bạn là chi tiết nhất và chính xác trong số những người khác.

Điểm chung là bạn không cần phải tạo hộp thoại theo cách thủ công, bạn chỉ cần 1) tạo một lớp con của DialogPreference sẽ xử lý logic của một tùy chọn phức tạp và 2) tạo một nút thuộc loại thích hợp trong your preferences.xml để hộp thoại sẽ được sinh ra tự động.

Vấn đề của SDK Android là bạn không thể thêm nút thích hợp đó bằng trình soạn thảo XML trực quan, bạn cần phải đi và chỉnh sửa tệp theo cách thủ công.

Vấn đề của tài liệu là nó bỏ sót rất ít thông tin này.

Vì vậy, đây là bước-by-step giải pháp:

1) Tạo một lớp con của DialogPreference mà sẽ xử lý ưu đãi đặc biệt của bạn. Để biết chi tiết về những gì cần thiết trong lớp con của bạn, tôi khuyên bạn nên this answer.

2) Tạo nút Sở thích trong tệp preferences.xml của bạn.

3) Chỉnh sửa tệp preferences.xml và thay thế Tùy chọn bằng tên đầy đủ của lớp con DialogPreference bao gồm đường dẫn gói, e. g. com.sample.MyPreferenceDialog. Bạn cũng có thể thêm một số thuộc tính vào nút để tùy chỉnh hộp thoại (tiêu đề, biểu tượng, v.v.), xem this answer hoặc tài liệu cho DialogPreference để biết chi tiết.

Đó là tất cả. Bạn không cần phải thêm OnPreferenceClickListener vào các tùy chọn, hộp thoại sẽ tự động hiển thị.

Lưu ý: Tôi không chắc chắn 100% rằng đây là cách dự định sử dụng mọi thứ, nhưng có vẻ như nó đang hoạt động.

Các vấn đề liên quan