无法在edittext中设置自定义图像

时间:2015-08-29 07:56:10

标签: android android-edittext textwatcher

我试图获得与here中显示的相同的结果。但是没有设置图像。此外,我必须单击每个edittext以键入数字。我想让光标在输入上一个时移动到下一个edittext(maxLength = 1)。 " star1"是带有星星和"空的"是没有明星的形象。如果有人能告诉我我做错了什么会非常有帮助。



activity_main

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

              <TableRow 
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal" >

                   

                    <EditText 
                        android:id="@+id/ET1" 
                        android:layout_height="70dp" 
                        android:layout_width="70dp" 
                        android:singleLine="true"
                        android:maxLength="1"
                        android:inputType="number"
                        android:background="@drawable/round" >
                    </EditText>

                   

                    <EditText 
                        android:id="@+id/ET2"
                        android:layout_height="70dp"  
                        android:layout_width="70dp"
                        android:singleLine="true"
                        android:maxLength="1"
                        android:inputType="number"
                        android:background="@drawable/round" >
                    </EditText>
                    
                    <EditText 
                        android:id="@+id/ET3"
                        android:layout_height="70dp"  
                        android:layout_width="70dp"
                        android:singleLine="true"
                        android:maxLength="1"
                        android:inputType="number"
                        android:background="@drawable/round" >
                    </EditText>
                    
                    <EditText 
                        android:id="@+id/ET4"
                        android:layout_height="70dp"  
                        android:layout_width="70dp"
                        android:singleLine="true"
                        android:maxLength="1"
                        android:inputType="number"
                        android:background="@drawable/round"  >
                    </EditText>

            </TableRow>

        </TableLayout>

       
    
    
    

</LinearLayout>
&#13;
Main Activity

public class MainActivity extends Activity {
	
	EditText ET1, ET2, ET3, ET4;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		ET1 = (EditText)findViewById(R.id.ET1);
		ET2 = (EditText)findViewById(R.id.ET2);
		ET3 = (EditText)findViewById(R.id.ET3);
		ET4 = (EditText)findViewById(R.id.ET4);
	} // onCreate ends
	
	
	private void handleEdittextListner(){
        try {
            ET1.addTextChangedListener(new TextWatcher() {
 
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    // TODO Auto-generated method stub
 
                }
 
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                        int after) {
                    // TODO Auto-generated method stub
 
                }
 
               

				@Override
				public void afterTextChanged(Editable s) {
					// TODO Auto-generated method stub
					if(ET1.getText().toString().trim().length()==1){
						 
                        ET1.clearFocus();
                        ET2.requestFocus();
                        ET1.setBackgroundResource(R.drawable.star1);
                    }
				}
            });
 
            ET2.addTextChangedListener(new TextWatcher() {
 
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    // TODO Auto-generated method stub
 
                }
 
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                        int after) {
                    // TODO Auto-generated method stub
 
                }
 
                @Override
                public void afterTextChanged(Editable s) {
                    if(ET2.getText().toString().trim().length()==1){
 
                        ET2.clearFocus();
                        ET3.requestFocus();
                        ET2.setBackgroundResource(R.drawable.star1);
                    }
                }
            });
            ET3.addTextChangedListener(new TextWatcher() {
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                }
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                        int after) {
                }
                @Override
                public void afterTextChanged(Editable s) {
                    if(ET3.getText().toString().trim().length()==1){
 
                        //ET1.clearFocus();
                        ET3.clearFocus();
                        ET4.requestFocus();
 
                        ET3.setBackgroundResource(R.drawable.star1);
                    }
 
                }
            });
            
            ET4.addTextChangedListener(new TextWatcher() {
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                }
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                        int after) {
                }
                @Override
                public void afterTextChanged(Editable s) {
                    if(ET4.getText().toString().trim().length()==1){
                        ET4.setBackgroundResource(R.drawable.star1);
                        //Hide keyboard
                        InputMethodManager imm =
                                (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.hideSoftInputFromWindow(ET4.getWindowToken(), 0);
                    }
 
                }
            });
            this.ET2.setOnKeyListener(new View.OnKeyListener()
            {

				@Override
				public boolean onKey(View paramView, int paramInt, KeyEvent paramKeyEvent) {
					// TODO Auto-generated method stub
					if ((paramKeyEvent.getAction() == KeyEvent.ACTION_DOWN)&&(paramInt == 67) && (MainActivity.this.ET2.getText().length() == 0)){
                        ET1.requestFocus();
                        ET1.setBackgroundResource(R.drawable.empty);
                        ET1.setText("");
                    }
					return false;
				}
                
            });
            this.ET3.setOnKeyListener(new View.OnKeyListener()
            {
                public boolean onKey(View paramView, int paramInt, KeyEvent paramKeyEvent)
                {
                    if ((paramKeyEvent.getAction() == KeyEvent.ACTION_DOWN)&&(paramInt == 67) && (MainActivity.this.ET3.getText().length() == 0)){
                        ET2.requestFocus();
                        ET2.setBackgroundResource(R.drawable.empty);
                        ET2.setText("");
                    }
 
                    return false;
                }
            });
            this.ET4.setOnKeyListener(new View.OnKeyListener()
            {
                public boolean onKey(View paramView, int paramInt, KeyEvent paramKeyEvent)
                {
                    if ((paramKeyEvent.getAction() == KeyEvent.ACTION_DOWN)&&(paramInt == 67) && (MainActivity.this.ET4.getText().length() == 0)){
                        ET3.requestFocus();
                        ET3.setBackgroundResource(R.drawable.empty);
                        ET3.setText("");
                    }
 
                    return false;
                }
            });
            
        } catch (Exception e) {
            e.toString();
        }
    }

	
} // Activity ends
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

首先在布局xml文件中为您的根布局元素设置白色背景颜色,如:

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

在所有EditText之后,为文本设置相同的颜色:

android:textColor="@android:color/white" 

之后做了类似的事情:

EditText ET1, ET2, ET3, ET4;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ET1 = (EditText)findViewById(R.id.ET1);
    ET2 = (EditText)findViewById(R.id.ET2);
    ET3 = (EditText)findViewById(R.id.ET3);
    ET4 = (EditText)findViewById(R.id.ET4);

    ET1.addTextChangedListener(new TextWatcher() {

        public void afterTextChanged(Editable s) {
            if(s.length() == 1){
                ET1.setBackgroundResource(R.drawable.star1);
                if(ET2.requestFocus()){
                    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                }
            }
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            // TODO Auto-generated method stub
        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub  
        }
    });

    ET2.addTextChangedListener(new TextWatcher() {

        public void afterTextChanged(Editable s) {
            if(s.length() == 1){
                ET2.setBackgroundResource(R.drawable.star1);
                if(ET3.requestFocus()){
                    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                }
            }
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            // TODO Auto-generated method stub
        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub  
        }
    });

    ET3.addTextChangedListener(new TextWatcher() {

        public void afterTextChanged(Editable s) {
            if(s.length() == 1){
                ET3.setBackgroundResource(R.drawable.star1);
                if(ET4.requestFocus()){
                    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                }
            }
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            // TODO Auto-generated method stub
        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub  
        }
    });

    ET4.addTextChangedListener(new TextWatcher() {

        public void afterTextChanged(Editable s) {
            if(s.length() == 1){
                ET3.setBackgroundResource(R.drawable.star1);
                InputMethodManager imm =
                        (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(ET4.getWindowToken(), 0);
            }
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            // TODO Auto-generated method stub
        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub  
        }
    });
}

修改

尝试将EditText放入FrameLayout。在每个FrameLayoutandroid:background="@drawable/round"中,例如:

<FrameLayout 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:background="@drawable/round" >
    <EditText 
        android:id="@+id/ET1" 
        android:layout_height="70dp" 
        android:layout_width="70dp" 
        android:singleLine="true"
        android:maxLength="1"
        android:inputType="number">
        </EditText>
    </FrameLayout>