0

在我的应用程序中,我动态生成一个包含编辑文本和一些按钮的线性布局。

我希望当用户按下设备键盘上的“输入”时,键盘应该隐藏

我通过设置 edittext 的输入类型来尝试它,但是当用户按下“enter”时,它会移动到另一个 edittext 。

我怎样才能做到这一点??

package com.integrated.mpr;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;


public class Page1 extends Activity implements OnClickListener{


    static String partname;
    int pos = StaticError.n;
    int ns= StaticError.ns;
    String s = StaticError.s;
    int[][] id = new int[pos][6];
    int submit ;

    double timedataa[] = {0,0,0,0,0};
    double timedatab[] = {0,0,0,0,0};
    double timedatac[] = {0,0,0,0,0};
    double timedata[] = {0,0,0,0,0};

    double rawdataa[] = new double[22050];
    double rawdatab[] = new double[22050];
    double rawdatac[] = new double[22050];
    double rawdata[] = new double[22050];


    FeatureExtract fe = new FeatureExtract();
    WavToDat wtd = new WavToDat();
    int i;


    int a=0;
    int b=0;
    int c=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);


        int p =0;
        Log.d("value of ", ""+pos);
        //Creating Different ids for the elements for different layouts
        for(int i =0;i<pos;i++){
            for(int j =0;j<6;j++){
                id[i][j] = p;
                p++;
            }
        }

        ScrollView sv = new ScrollView(this);



        LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        int resid = getResources().getIdentifier("background", "drawable", getPackageName());
        ll.setBackgroundResource(resid);

        Button ins = new Button(this);
        ins.setText("Instructions");
        ins.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
        ins.setGravity(Gravity.RIGHT);
        ins.setId(5000);
        ins.setOnClickListener(new OnClickListener() 
        {
            public void onClick(View v) 
            {
                // TODO Auto-generated method stub
                Context mContext = Page1.this;
                Dialog dialog = new Dialog(mContext);
                dialog.setTitle("Instructions");
                dialog.setContentView(R.layout.instructiondialog);
                dialog.show();
            }
        });

        ins.setTextColor(Color.BLUE);
        ins.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
        ins.setTypeface(Typeface.SERIF);
        ll.addView(ins);


        for(i=0;i<pos;i++){



            LinearLayout llay = new LinearLayout(this);
            llay.setOrientation(LinearLayout.VERTICAL);
            llay.setId(id[i][5]);

            //EDIT TEXT
            EditText et = new EditText(this);
            et.setId(id[i][0]);
            et.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
            et.setTypeface(Typeface.SERIF);



            LinearLayout lhor = new LinearLayout(this);
            lhor.setOrientation(LinearLayout.HORIZONTAL);
            lhor.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
            lhor.setWeightSum(90);


            Button starta = new Button(this);
            starta.setText("Record 1");
            starta.setId(id[i][1]);
            starta.setOnClickListener(this);


            Button startb = new Button(this);
            startb.setText("Record 2");
            startb.setId(id[i][2]);

            startb.setOnClickListener(this);


            Button startc = new Button(this);
            startc.setText("Record 3");
            startc.setId(id[i][3]);

            startc.setOnClickListener(this);

            lhor.addView(starta);
            lhor.addView(startb);
            lhor.addView(startc);

            Button stop = new Button(this);
            stop.setText("Submit");
            stop.setId(id[i][4]);

            stop.setOnClickListener(this);


            TextView tv1 = new TextView(this);
            tv1.setVisibility(llay.INVISIBLE);

            llay.addView(tv);
            llay.addView(et);
            llay.addView(lhor);
            llay.addView(stop);
            llay.addView(tv1);
            ll.addView(llay);

        }



        sv.addView(ll);
        this.setContentView(sv);


    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub





        }





}
4

1 回答 1

2

尝试以下操作:

final EditText et = new EditText(this);
        et.setId(id[i][0]);
        et.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
        et.setTypeface(Typeface.SERIF);

et.setOnKeyListener(new View.OnKeyListener()
        {

            @Override
            public boolean onKey(View editView, int keyCode, KeyEvent event)
            {
                Context mContext = Page1.this;
                if( keyCode == KeyEvent.KEYCODE_ENTER ){
                    et.clearFocus();
                    InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(et.getWindowToken(),0);
                    return true;
                }
                return false;
            }
        })
于 2012-06-11T06:23:07.407 回答