0

主要活动

   public class MainActivity extends Activity {
// Declare our Views, so we can access them later
private CheckUsernameEditText etUsername;
private EditText etPassword;
private EditText etPassword2;
private Button btnRegister;
private Button btnCancel;
private TextView lblUserStatus;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Set Activity Layout
    setContentView(R.layout.activity_main);

    // Get the EditText and Button References
    etUsername = (CheckUsernameEditText) findViewById(R.id.username);
    etPassword = (EditText) findViewById(R.id.password);
    etPassword2 = (EditText) findViewById(R.id.password2);
    btnRegister = (Button) findViewById(R.id.register_button);
    btnCancel = (Button) findViewById(R.id.cancel_button);
    lblUserStatus = (TextView) findViewById(R.id.userstatus);

    // Set our new Listener to the Username EditText view
    etUsername.setOnUsernameAvailableListener(new OnUsernameAvailableListener() {
                @Override
                public void onAvailableChecked(String username,
                        boolean available) {
                    // Handle the event here
                    if (!available) {
                        etUsername.setTextColor(Color.RED);
                        lblUserStatus
                                .setText(username
                                        + " is already taken. Please choose another login name.");
                    } else {
                        etUsername.setTextColor(Color.GREEN);
                        lblUserStatus.setText(username + " is available.");
                    }
                }
            });

    // Set Click Listener
    btnRegister.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // create Account
        }
    });
    btnCancel.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // Close the application
            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" >

           *
           *
<EditText
    android:id="@+id/username"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:singleLine="true" />

           *
           *
</LinearLayout>

检查用户名编辑文本

   public class CheckUsernameEditText extends EditText implements OnKeyListener {

OnUsernameAvailableListener onUsernameAvailableListener = null;
final private static String[] registeredUsers = new String[] {
        // This is just a fixed List for tutorial purposes
        // in a real application you'd check this server sided or inside the
        // database
        "tseng", "admin", "root", "joedoe", "john" };

   public CheckUsernameEditText(Context context) {
    super(context);
    // Set KeyListener to ourself
    this.setOnKeyListener(this);
}

public CheckUsernameEditText(Context context, AttributeSet attrs,
        int defStyle) {
    super(context, attrs, defStyle);
    // Set KeyListener to ourself
    this.setOnKeyListener(this);
}

public CheckUsernameEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
    // Set KeyListener to ourself
    this.setOnKeyListener(this);
}

// Allows the user to set an Listener and react to the event
public void setOnUsernameAvailableListener(
        OnUsernameAvailableListener listener) {
    onUsernameAvailableListener = listener;
}

// This function is called after the check was complete
private void OnUserChecked(String username, boolean available) {
    // Check if the Listener was set, otherwise we'll get an Exception when
    // we try to call it
    if (onUsernameAvailableListener != null) {
        // Only trigger the event, when we have a username
        if (!TextUtils.isEmpty(username)) {
            onUsernameAvailableListener.onAvailableChecked(username,
                    available);
        }
    }
}

@Override
public boolean onKey(View v, int keycode, KeyEvent keyevent) {
    // We only want to handle ACTION_UP events, when user releases a key
    if (keyevent.getAction() == KeyEvent.ACTION_DOWN)
        return false;

    boolean available = true;

    // Whenever a user press a key, check if the username is available
    String username = getText().toString().toLowerCase();
    if (!TextUtils.isEmpty(username)) {
        // Only perform check, if we have anything inside the EditText box
        for (int i = 0; i < registeredUsers.length; i++) {
            if (registeredUsers[i].equals(username)) {
                available = false;
                // Finish the loop, as the name is already taken
                break;
            }
        }
        // Trigger the Event and notify the user of our widget
        OnUserChecked(username, available);
        return false;
    }
    return false;
}

// Define our custom Listener interface
public interface OnUsernameAvailableListener {
    public abstract void onAvailableChecked(String username,
            boolean available);
}
   }

问题是我接受了一个类分类异常。因为我在 xml 中将用户名声明为 edittext,而在主代码中我将其声明为 CheckUsernameEditText。我该如何解决这个问题?为什么转换不起作用,尤其是现在 CheckUsernameEditText 扩展了 EditText 类?

4

2 回答 2

5

所有CheckUsernameEditText对象都是EditText对象,
并非所有EditText对象都是CheckUsernameEditText对象。

您应该在 XML 中使用您的自定义类:

<your.package.name.CheckUsernameEditText
    android:id="@+id/username"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:singleLine="true" />
于 2012-11-29T17:50:57.687 回答
2

我相信如果您有一个自定义视图(在您的情况下为 CheckUsernameEditText),您必须在 XML 中声明它......请记住,正如@Sam 指出的那样,您不能向下转换为派生类,您只能向上转换到父类中,因此您始终可以将 CheckUsernameEditText 视图转换为 EditText (或只是视图),但您不能采用其他方式。

于 2012-11-29T17:51:18.137 回答