2012-11-22 38 views
20

Chúng tôi có thể đặt lỗi trong Edittext thành công nhưng không đặt được trong chế độ xem văn bản. có vấn đề gì không?? tôi đã thửAndroid setError ("lỗi") không hoạt động trong Textview

((TextView) findViewById(R.id.df)).requestFocus(); 
((TextView) findViewById(R.id.df)).setSelected(true); 
((TextView) findViewById(R.id.df)).setError("akjshbd"); 

nhưng tôi không nhận được cửa sổ bật lên do lỗi.

Textview Error

+0

Popup được cho EditText –

+0

@Anis, điều này được viết bằng http://developer.android.com/reference /android/widget/TextView.html, setError đó (lỗi CharSequence): Đặt hợp chất có thể vẽ bên phải của TextView thành biểu tượng "lỗi" và đặt thông báo lỗi sẽ được hiển thị trong cửa sổ bật lên khi TextView đã tập trung . –

Trả lời

29

Thực ra, bạn có thể sử dụng setError cho văn bảnXem và hiển thị cửa sổ bật lên.

Bạn chỉ cần sử dụng cùng một kiểu với EditText.

Chỉ cần thêm thuộc tính tiếp theo cho TextView trong xml:

style="@android:style/Widget.EditText" 
+4

[câu trả lời của nayoga0] (http://stackoverflow.com/a/15954372/2209946) là tốt hơn nhiều so với điều này. –

+0

@DavidLord Bằng cách nào? –

5

Đoạn trích: bạn phải requestFocus(); một cái nhìn để hiển thị lỗi.

// Check for a valid email address. 
if (TextUtils.isEmpty(mEmail)) { 
    mEmailView.setError(getString(R.string.error_field_required)); 
    focusView = mEmailView; 
    cancel = true; 
} else if (!mEmail.contains("@")) { 
    mEmailView.setError(getString(R.string.error_invalid_email)); 
    focusView = mEmailView; 
    cancel = true; 
} 

if (cancel) { 
    // There was an error; don't attempt login and focus the first 
    // form field with an error. 
    focusView.requestFocus(); 
} else { 
    // Show a progress spinner, and kick off a background task to 
    // perform the user login attempt. 
    // showProgress(true); 
    // mAuthTask = new UserLoginTask(); 
    // mAuthTask.execute((Void) null); 
    ParseUser.logInInBackground(mEmail, mPassword, new LogInCallback() { 

    @Override 
    public void done(ParseUser user, ParseException e) { 
     finishAndStartCardActivity(); 
    } 
    }); 
} 
+1

editText.requestFocus(); editText.setError ("bất cứ điều gì"); – Maragues

68

Mặc định TextView là không thể đặt tiêu điểm. Vì vậy, bạn cần đặt android: focusable = "true"android: focusableInTouchMode = "true".

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:focusable="true" 
    android:focusableInTouchMode="true" 
    android:text="@string/hello_world" /> 

Và không cần đặt setSelected (true).

((TextView) findViewById(R.id.df)).requestFocus(); 
((TextView) findViewById(R.id.df)).setError("akjshbd"); 
+2

Tôi thích giải pháp này. Đặt kiểu thành @android: style/Widget.EditText là quá mức cần thiết và làm cho TextView trông giống như EditText. – starkej2

+3

Giải pháp này sạch hơn nhiều so với câu trả lời được chấp nhận. – Ridcully

+0

Đây là giải pháp tốt hơn, không nghi ngờ gì – Gonzalo

10

Đây là duy nhất bạn cần để có được dự kiến ​​hành vi setError trên TextView

android:focusable="true" 
android:clickable="true" 
android:focusableInTouchMode="true" 
+0

Câu trả lời này cũng hoạt động. Điều này là đơn giản nhất trong số tất cả. Cảm ơn –

+1

đủ đơn giản và không thể hiện phong cách editText phiền toái đó! – Choletski

+0

nhưng nó cũng mở bàn phím khi nhấp vào chế độ xem văn bản – seema

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