设置Android EditText的高度不起作用,它涵盖了整个屏幕

时间:2014-02-23 18:27:22

标签: android android-edittext

我正在尝试在Android中设置EditText的高度和宽度,但它不起作用。它涵盖了整个屏幕。

我想要的是制作一个圆角的小方块。在其中,应该可以写出最多10个字母,当用户按下回车键时,它将关闭。

这些都不是直截了当的。

public static EditText makeEditText( final Context context, String text, int x, int y, int id, int w, int h,
        int textSize, Resources res, int resource_id, final GameFragment gameFragment,
        final HighscoresDatabaseHandler dbhandler, final int level, final int time )
{
    final EditText nameEdit = new EditText( context );
    nameEdit.setWidth( w );
    nameEdit.setHeight( h );
    nameEdit.setAllCaps( true ); 
    nameEdit.setTextSize( textSize );
    nameEdit.setTextColor( 0xFF7F5000 );
    //nameEdit.setBackground( res.getDrawable( resource_id ) );
    nameEdit.setX( x );
    nameEdit.setY( y );
    nameEdit.setAlpha( Constants.ALPHA_OPAQUE );
    nameEdit.setText( text );
    nameEdit.setId( id );

    nameEdit.setOnEditorActionListener(
            new EditText.OnEditorActionListener() 
            {
                @Override
                public boolean onEditorAction( TextView v, int actionId, KeyEvent event ) 
                {
                    if ( actionId == EditorInfo.IME_ACTION_SEARCH || 
                            actionId == EditorInfo.IME_ACTION_DONE ||
                            event.getAction() == KeyEvent.ACTION_DOWN &&
                            event.getKeyCode() == KeyEvent.KEYCODE_ENTER) 
                    {
                        if ( !event.isShiftPressed() ) 
                        {
                            gameFragment.setPlayerName( v.getText().toString() );
                            dbhandler.updateHighscore( new Highscore( 
                                    level, 
                                    v.getText().toString(), 
                                    time ) ); 
                            nameEdit.clearFocus();
                            ((ViewGroup) ((MainActivity)context).findViewById( android.R.id.content )).removeView( nameEdit );

                            FragmentManager fragmentManager = ((MainActivity)context).getSupportFragmentManager();
                            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                            fragmentTransaction.replace( R.id.fragment_content, new ChooseLevelFragment(), "chooselevel" );
                            fragmentTransaction.commit();

                            return true;
                        }                
                    }

                    return false;
                }
            });

    return nameEdit;
}



                EditText nameEdit = ButtonUtils.makeEditText( 
                    getActivity(), 
                    "Enter name", 
                    30,
                    30, 
                    RESULT_BUTTON_ID+1, 
                    100, 
                    50,
                    15, 
                    getResources(), 
                    R.layout.start_finish_button,
                    this,
                    dbHandler,
                    theLevel,
                    time );
            ((ViewGroup) (theActivity).findViewById( android.R.id.content )).addView( nameEdit );

1 个答案:

答案 0 :(得分:0)

尝试使用

nameEdit.setLayoutParams(new LayoutParams(w,h));

而不是

nameEdit.setWidth( w );
nameEdit.setHeight( h );