如何以编程方式在线性布局中找到editext?

时间:2014-02-21 11:54:17

标签: android

Original image

edited image

如何找到附近的edittext TextView(“输入有线数据”)? 这是我的代码..可以在LinearLayout

中执行此操作
public class Ybus_Activity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ybus);
final LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT,
        LayoutParams.WRAP_CONTENT);
final LinearLayout main = (LinearLayout)findViewById(R.id.android_main_layout);
TextView getData=new TextView(this);
getData.setText("Enter the number of LineData : ");
getData.setId(5);
getData.setLayoutParams(params);
main.addView(getData);
final EditText edText = new EditText(this);
edText.setId(3);
edText.setLayoutParams(params);
edText .setWidth(100);
edText .setImeOptions(EditorInfo.IME_ACTION_NEXT);
edText .setInputType(InputType.TYPE_CLASS_NUMBER);
edText .setKeyListener(DigitsKeyListener.getInstance());
edText .setMaxLines(1);
main.addView(edText );
Button bt = new Button(this);
bt.setText("Click to enter Linedata");
bt.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
main.addView(bt);
final TextView text = new TextView(this);
bt.setOnClickListener(new View.OnClickListener() 
{ 
    public void onClick(View v) 
    {
        String ed=edText.getText().toString(); 
        int i = 0;
        try{
          i =Integer.parseInt(ed);
         //setting value here
          text.setText(i+"");
         //or you can do like this
         //text.setText(String.valueOf(i));
        }catch(NumberFormatException ex){
          text.setText("Value at TextView is not a valid integer");
        }
    }
});
main.addView(text);
 }
}

2 个答案:

答案 0 :(得分:2)

创建相对布局而不是线性布局。尝试类似这样的

RelativeLayout.LayoutParams pSS = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT
                            ,
                            LayoutParams.FILL_PARENT);
                    pSS.addRule(RelativeLayout.Right, id of the textview);
                    pSS.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

                    edText .setLayoutParams(pSS);

答案 1 :(得分:0)

试试这个

final LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT);
    final LinearLayout main = (LinearLayout)findViewById(R.id.android_main_layout);

    LinearLayout layout = new LinearLayout(this);
    layout.setLayoutParams( new LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT));
    layout.setOrientation(LinearLayout.HORIZONTAL);


    TextView getData=new TextView(this);
    getData.setText("Enter the number of LineData : ");
    getData.setId(5);
    getData.setLayoutParams(params);
    layout.addView(getData);
    final EditText edText = new EditText(this);
    edText.setId(3);
    edText.setLayoutParams(params);
    edText .setWidth(100);
    edText .setImeOptions(EditorInfo.IME_ACTION_NEXT);
    edText .setInputType(InputType.TYPE_CLASS_NUMBER);
    edText .setKeyListener(DigitsKeyListener.getInstance());
    edText .setMaxLines(1);
    layout.addView(edText );
    Button bt = new Button(this);
    bt.setText("Click to enter Linedata");
    bt.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

    main.addView(layout);
    main.addView(bt);
    final TextView text = new TextView(this);
    bt.setOnClickListener(new View.OnClickListener() 
    { 
        public void onClick(View v) 
        {
            String ed=edText.getText().toString(); 
            int i = 0;
            try{
              i =Integer.parseInt(ed);
             //setting value here
              text.setText(i+"");
             //or you can do like this
             //text.setText(String.valueOf(i));
            }catch(NumberFormatException ex){
              text.setText("Value at TextView is not a valid integer");
            }
        }
    });
    main.addView(text);