2012-02-20 30 views
11

Tôi có bốn EditText với nền khác nhau. Có vẻ như sau: enter image description hereThay đổi chiều cao và chiều rộng EditText trên Thời gian chạy bằng một số hoạt ảnh

Tôi muốn bao gồm toàn màn hình khi EditText được chọn với EditText đó. Cho rằng tôi cần phải thay đổi chiều rộng và chiều cao EditText với một số hình ảnh động trên Runtime.

Khi được chọn Nó sẽ giống như thế này: enter image description here

Làm thế nào tôi có thể thay đổi kích thước EditText với hình ảnh động trên Runtime?

+0

như vậy, bạn cần phải sử dụng hình ảnh động để làm cho EditText chiếm mẹ điền bố trí khi yêu cầu tập trung vào nó. đúng? –

+0

@Raman yes.I đã thử với ans của bạn, Nhưng nó không phải là điều đó. – asish

Trả lời

4

Tôi không chắc chắn rằng nó có thể được thực hiện với Animations. Trong chế độ xem android mất tất cả không gian trước khi hoạt ảnh kết thúc, vì vậy bạn sẽ thấy rằng các editTexts khác biến mất và chọn một từ từ tăng dần. Dưới đây là ví dụ thô làm thế nào để làm điều đó mà không cần hình ảnh động standart, nhưng thay đổi trọng lượng trong chủ đề riêng biệt:

bố trí:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<LinearLayout 
    android:id="@+id/ll0" 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:orientation="horizontal" > 

    <EditText 
     android:id="@+id/et00" 
     android:layout_width="0dp" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:gravity="top|left" 
     android:text="00" /> 

    <EditText 
     android:id="@+id/et01" 
     android:layout_width="0dp" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:gravity="top|left" 
     android:text="01" /> 
</LinearLayout> 

<LinearLayout 
    android:id="@+id/ll1" 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:orientation="horizontal" > 

    <EditText 
     android:id="@+id/et10" 
     android:layout_width="0dp" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:gravity="top|left" 
     android:text="10" /> 

    <EditText 
     android:id="@+id/et11" 
     android:layout_width="0dp" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:gravity="top|left" 
     android:text="11" /> 
</LinearLayout> 
</LinearLayout> 

và trong mã thêm hành động về việc thay đổi trọng tâm:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    OnFocusChangeListener focusListener = new OnFocusChangeListener() { 
     @Override 
     public void onFocusChange(View v, boolean hasFocus) { 
      if (hasFocus) { 
       LinearLayout parent = (LinearLayout) v.getParent(); 
       EditText forDecresing = null; 
       for (int i = 0; i < parent.getChildCount(); i++) { 
        if (parent.getChildAt(i) != v) { 
         forDecresing = (EditText) parent.getChildAt(i); 
         break; 
        } 
       } 
       LinearLayout pp = (LinearLayout) parent.getParent(); 
       LinearLayout layoutForDecreasing = null; 
       for (int i = 0; i < pp.getChildCount(); i++) { 
        if (pp.getChildAt(i) != parent && pp.getChildAt(i) instanceof LinearLayout) { 
         layoutForDecreasing = (LinearLayout) pp.getChildAt(i); 
         break; 
        } 
       } 
       startAnimation((EditText) v, forDecresing, layoutForDecreasing, parent); 
      } else { 
      } 
     } 
    }; 

    ((EditText) findViewById(R.id.et00)).setOnFocusChangeListener(focusListener); 
    ((EditText) findViewById(R.id.et01)).setOnFocusChangeListener(focusListener); 
    ((EditText) findViewById(R.id.et11)).setOnFocusChangeListener(focusListener); 
    ((EditText) findViewById(R.id.et10)).setOnFocusChangeListener(focusListener); 
} 

public void onBackPressed() { 
    setWeight(findViewById(R.id.et00), 1); 
    setWeight(findViewById(R.id.et01), 1); 
    setWeight(findViewById(R.id.et11), 1); 
    setWeight(findViewById(R.id.et10), 1); 
    setWeight(findViewById(R.id.ll1), 1); 
    setWeight(findViewById(R.id.ll0), 1); 
} 

Thread animationThread; 

private void startAnimation(final EditText forIncreasing, final EditText forDecresing, final LinearLayout layoutForDecreasing, 
     final LinearLayout layoutForIncreasing) { 
    if (animationThread != null) 
     animationThread.interrupt(); 
    animationThread = new Thread(new Runnable() { 
     @Override 
     public void run() { 
      int iterations = 0; 
      int maxIterations = 30; 
      setWeight(forIncreasing, maxIterations - 1); 
      setWeight(layoutForIncreasing, maxIterations - 1); 
      setWeight(forDecresing, maxIterations - 1); 
      setWeight(layoutForDecreasing, maxIterations - 1); 
      while (iterations < maxIterations) { 
       iterations++; 
       setWeight(forIncreasing, maxIterations - 1 + iterations); 
       setWeight(layoutForIncreasing, maxIterations - 1 + iterations); 
       setWeight(forDecresing, maxIterations - 1 - iterations); 
       setWeight(layoutForDecreasing, maxIterations - 1 - iterations); 
       try { 
        Thread.sleep(10); 
       } catch (InterruptedException e) { 
        return; 
       } 
      } 
      animationThread = null; 
     } 
    }); 
    animationThread.start(); 
} 

private void setWeight(final View view, final float weight) { 
    runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      LayoutParams params = (LayoutParams) view.getLayoutParams(); 
      params.weight = weight; 
      view.setLayoutParams(params); 
     } 
    }); 
} 

tôi làm không biết nếu điều này là có thể cho bạn, nhưng trong ví dụ này bạn có thể thêm một số chuyển động khá dễ dàng.

tôi làm không khuyên sử dụng nó sản xuất, nếu chỉ có bạn có một số lựa chọn khác.

+0

Xem câu trả lời đã chỉnh sửa của tôi – Jin35

0

Tôi đã cố gắng để giải thích logic của tôi cho vấn đề của bạn ... tôi hy vọng như vậy nó sẽ làm việc cho bạn ..

EditText editText = new EditText(this); 
editText.setOnClickListener(new OnClickListener() { 

    public void onClick(){  
     // Before getSize was introduced (in API level 13), you could use 
     // the getWidth and getHeight methods that are now deprecated: 

     Display display = getWindowManager().getDefaultDisplay(); 
     int width = display.getWidth(); 
     int height = display.getHeight(); 

     editText.setWidth(width); 
     editText.setHeight(height); 
    } 
} 
+1

Đây là những gì tôi biết bạn thân mến của tôi ... tôi xin lỗi tôi có thể giúp bạn furture .... nhưng vẫn .... bạn hãy thử với khái niệm này cho Animation ... đây là cách đúng để thay đổi kích thước khi chạy và cũng sẽ hữu ích khi thay đổi kích thước của hoạt ảnh ... –

6

ĐÁP CẬP NHẬT:

sử dụng mã này trong drawable /animation_sample.xml

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false"> 
<translate android:fromXDelta="0%" android:toXDelta="100%" android:fromYDelta="0%" android:toYDelta="0%" android:duration="500" /> 

Và thiết lập các hình ảnh động để soạn thảo văn bản sử dụng:

Animation anim=AnimationUtils.loadAnimation(this, R.anim.righttoleft); 
editText = (EditText)findViewById(R.id.editText1); 
Display display = getWindowManager().getDefaultDisplay(); 
int width=display.getWidth(); 
int height = display.getHeight(); 
editText.setWidth(width); 
editText.setHeight(height); 

editText.startAnimation(anim); 
Các vấn đề liên quan