2016-08-11 9 views
0

私はアプリケーションで作業しており、EditTextをXMLファイルに取得できません。xmlファイルにEditTextを追加すると動作しません

ScrollViewを追加しても機能しませんでしたので、削除して画面全体にグループを広げましたが、修正できません。

PlayScreen.java

/** 
* Created by Gavin on 8/10/2016. 
*/ 
public class PlayScreen extends Activity { 

    public EditText numberOP; 
    private RelativeLayout layout; 

    private int i = 0; 
    private int x = 0; 
    private int y = 1; 
    private int z = 1; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

     setContentView(R.layout.layout_play); 

     numberOP = (EditText) findViewById(R.id.number_of_players); 

     layout = (RelativeLayout) findViewById(R.id.layout_play_id); 

    } 

    public void onEnterClickOne(View view) { 

     /**if(textView != null) { 
      layout.removeView(textView); 
     } 
     if(editText != null) { 
      layout.removeView(editText); 
     }*/ 

     int b = 0; 

     if(i+y+x+z != 0) { 
      for (b = 0; b < i + y + x + z * 100; b++) { 
       System.out.println(b); 
       View v = findViewById(b); 
       if (v != null) { 
        layout.removeView(v); 
       } 
      } 
     } 

     i = 0; 
     x = 0; 
     y = 1; 
     z = 1; 

     if (numberOP.getText().toString().length() > 0) { 

      int length = Integer.parseInt(numberOP.getText().toString())*2; 

      for (i = 0; i < length; i++) { 
       if (i % 2 == 0) { 
        x++; 
        TextView textView = new TextView(PlayScreen.this); 
        textView.setId(x*30); 
        textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT)); 
        textView.setText(String.format("Group: %s", Integer.toString(x))); 
        textView.setTextSize(20); 
        textView.setTypeface(null,Typeface.BOLD); 
        layout.addView(textView); 
       } 
       EditText editText = new EditText(PlayScreen.this); 
        editText.setId(i+1); 
        editText.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT)); 
       if(y == 1) { 
        editText.setHint("Partner 1"); 
        y++; 
       } else if(y == 2) { 
        editText.setHint("Partner 2"); 
        y--; 
       } 
       layout.addView(editText); 
      } 
     } 
    } 
} 

layout_play.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:fillViewport="true" 
    android:background="#f7f7f7" 
    android:id="@+id/layout_play_id" 
    android:padding="25dp"> 

    <TextView 
     android:text="@string/choose_your_partners" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="31sp" 
     android:layout_centerHorizontal="true" 
     android:id="@+id/editTextTwo" /> 

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:inputType="numberSigned" 
     android:ems="10" 
     android:hint="@string/number_of_groups" 
     android:id="@+id/number_of_players" 
     android:layout_below="@+id/editTextTwo" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" /> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/button_1_text" 
     android:textSize="30sp" 
     android:id="@+id/playButton" 
     android:onClick="onEnterClickOne" 
     android:layout_below="@+id/number_of_players" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" /> 
</RelativeLayout> 

あなたがScrollViewのための任意の提案を持っている場合にも、それは素晴らしいことです。ありがとう。ビューコンポーネントのような種類について

The Problem

+0

EditTextが表示されない、または何が表示されないのはどういう意味ですか? –

+0

TextViewとEditTextは、画面上で右上に表示されるのではなく、画面全体に広がります。 –

+0

問題を示す画像を投稿に追加しました。 –

答えて

0

、容易にこの内部のビューコンポーネントを置くために、垂直方向でLinearLayoutを使用する方がよいです。

RelativeLayout: RelativeLayoutを使用すると、子ビューの相対的な配置方法を指定できます。各ビューの位置は、兄弟要素に関連して、または親に対して相対的に指定することができます。

enter image description here

のLinearLayout: のLinearLayoutは垂直または水平方向にすべての子を揃えビューグループです。 enter image description here

+0

それを修正しました。私はまた、そのクラス内にScrollViewを追加し、それは動作するようです。 –

+0

答えがあなたの問題を解決したか、助けてくれたなら、答えを受け入れて投票してください。ありがとう –

関連する問題