2014-02-21 59 views
0

Original image如何以編程方式在線性佈局中查找editext?

edited image

如何查找edittext附近TextView( 「輸入linedata數量」)? 這是我code..Is有可能做這裏面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); 
} 
} 
+0

@Raghunandan如何兄弟??我是新來的Android .. – gkn06

+0

對不起,我誤解了這個問題。你想以編程方式移動textview旁邊的視圖? – Raghunandan

+0

如何將edittext移動到文本視圖 – gkn06

回答

2

產生相對的佈局,而不是線性layout.Try像這樣

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); 
+0

爲什麼RelativeLayout的??廣東話我的線性佈局做 – gkn06

+0

燁ü可以在線性佈局中做到這一點,但總是喜歡以相對的方式設置您的佈局 – goonerDroid

+0

您是否有任何想法通過線性佈局 – gkn06

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); 
相關問題