2016-11-27 2 views
0

나는 안드로이드 코딩에 아주 익숙하다. 버튼을 눌렀을 때 동적으로 텍스트 뷰를 만드는 것에 대한 질문이있다.버튼을 누를 때마다 새 Textviews를 어떻게 추가합니까?

버튼을 눌렀을 때 다른 활동에 textviews를 추가하는 방법을 알아 냈습니다. 버튼을 누를 때마다 매번 새로운 textviews를 추가하고 이전에 생성 된 이전 텍스트는 삭제하지 않으려합니다. 이 제품 ^^^

 RelativeLayout Mainlayout = (RelativeLayout) findViewById(R.id.activity_job_tracking); 
    RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
    RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
    RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
    RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
    RelativeLayout.LayoutParams params5 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
    RelativeLayout.LayoutParams params6 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
    Intent aa = getIntent(); 

     String a = aa.getStringExtra("NAME"); 
     String f = aa.getStringExtra("ADDRESS"); 
     String b = aa.getStringExtra("EMAIL"); 
     String c = aa.getStringExtra("PHONE"); 
     String d = aa.getStringExtra("MOBILE"); 
     String e = aa.getStringExtra("QUOTEDESCRIPTION"); 
     String g = aa.getStringExtra("JOBDESCRIPTION"); 
     TextView NAME = new TextView(this); 
     NAME.setId(vvv); 

     TextView ADDRESS = new TextView(this); 
     ADDRESS.setId(vvv + 1); 
     params1.addRule(RelativeLayout.BELOW, NAME.getId()); 
     ADDRESS.setLayoutParams(params1); 

     TextView EMAIL = new TextView(this); 
     EMAIL.setId(vvv + 2); 
     params2.addRule(RelativeLayout.BELOW, R.id.vvv + 1); 
     EMAIL.setLayoutParams(params2); 

     TextView PHONE = new TextView(this); 
     PHONE.setId(vvv + 3); 
     params3.addRule(RelativeLayout.BELOW, R.id.vvv + 2); 
     PHONE.setLayoutParams(params3); 

     TextView MOBILE = new TextView(this); 
     MOBILE.setId(vvv + 4); 
     params4.addRule(RelativeLayout.BELOW, R.id.vvv + 3); 
     MOBILE.setLayoutParams(params4); 

     TextView QUOTEDESCRIPTION = new TextView(this); 
     QUOTEDESCRIPTION.setId(vvv + 5); 
     params5.addRule(RelativeLayout.BELOW, R.id.vvv + 4); 
     QUOTEDESCRIPTION.setLayoutParams(params5); 

     TextView JOBDESCRIPTION = new TextView(this); 
     JOBDESCRIPTION.setId(vvv + 6); 
     params6.addRule(RelativeLayout.BELOW, R.id.vvv + 5); 
     JOBDESCRIPTION.setLayoutParams(params6); 

     NAME.setText("Client name " + a); 
     ADDRESS.setText("Client address "+ f); 
     EMAIL.setText("Client email "+ b); 
     PHONE.setText("Client phone number "+ c); 
     MOBILE.setText("Client mobile "+ d); 
     QUOTEDESCRIPTION.setText("Quote description " + e); 
     JOBDESCRIPTION.setText("Job Description "+ g); 
     Mainlayout.addView(NAME); 
     Mainlayout.addView(ADDRESS, params1); 
     Mainlayout.addView(EMAIL, params2); 
     Mainlayout.addView(PHONE, params3); 
     Mainlayout.addView(MOBILE, params4); 
     Mainlayout.addView(QUOTEDESCRIPTION, params5); 
     Mainlayout.addView(JOBDESCRIPTION, params6); 
     Font application = (Font) getApplication(); 
     application.setTypeface(NAME); 
     application.setTypeface(ADDRESS); 
     application.setTypeface(EMAIL); 
     application.setTypeface(PHONE); 
     application.setTypeface(MOBILE); 
     application.setTypeface(QUOTEDESCRIPTION); 
     application.setTypeface(JOBDESCRIPTION); 

    } 

는 문자열이 수신되는 경우, PARAMS 설정하고 지정 서체는 새로운 textviews인가된다.

final EditText NAME = (EditText) findViewById(R.id.editText); 
    final EditText ADDRESS = (EditText) findViewById(R.id.editText6); 
    final EditText EMAIL = (EditText) findViewById(R.id.editText2); 
    final EditText PHONE = (EditText) findViewById(R.id.editText4); 
    final EditText MOBILE = (EditText) findViewById(R.id.editText5); 
    final EditText QDESCRIPTION = (EditText) findViewById(R.id.editText3); 
    final EditText JDESCRIPTION = (EditText) findViewById(R.id.editText7); 
    final Button Donebutton = (Button) findViewById(R.id.button2); 
    Font application = (Font) getApplication(); 
    application.setTypeface(NAME); 
    application.setTypeface(ADDRESS); 
    application.setTypeface(EMAIL); 
    application.setTypeface(PHONE); 
    application.setTypeface(MOBILE); 
    application.setTypeface(QDESCRIPTION); 
    application.setTypeface(JDESCRIPTION); 
    Donebutton.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 


      final Intent aa = new Intent(MainActivity.this, JobTrackingActivity.class); 
      aa.putExtra("NAME", NAME.getText().toString()); 
      aa.putExtra("ADDRESS", ADDRESS.getText().toString()); 
      aa.putExtra("EMAIL", EMAIL.getText().toString()); 
      aa.putExtra("PHONE", PHONE.getText().toString()); 
      aa.putExtra("MOBILE", MOBILE.getText().toString()); 
      aa.putExtra("QUOTEDESCRIPTION", QDESCRIPTION.getText().toString()); 
      aa.putExtra("JOBDESCRIPTION", JDESCRIPTION.getText().toString()); 
      startActivity(aa); 
     } 
    }); 
} 

^^^ 이것은 문자열이 전송되는 부분입니다.

+0

recyclerView에 텍스트 뷰를 추가하는 것이 좋습니다. 버튼을 누를 때마다 새 카드가 추가됩니다. (이것은 스크롤 동작을 허용한다). –

답변

0
private LinearLayout mLayout; 
private EditText mEditText; 
private Button mButton; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 
mLayout = (LinearLayout) findViewById(R.id.linearLayout); 
mEditText = (EditText) findViewById(R.id.editText); 
mButton = (Button) findViewById(R.id.button); 
mButton.setOnClickListener(onClick()); 
TextView textView = new TextView(this); 
textView.setText("New text"); 

}

개인 OnClickListener를 온 클릭() { 반환 새로운 OnClickListener를() {

@Override 
    public void onClick(View v) { 
     mLayout.addView(createNewTextView(mEditText.getText().toString())); 
    } 
}; 

}

private TextView createNewTextView(String text) { 
final LayoutParams lparams = new 
LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
final TextView textView = new TextView(this); 
textView.setLayoutParams(lparams); 
textView.setText("New text: " + text); 
return textView; 

}

이 추가 xml

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:id="@+id/linearLayout"> 
<EditText 
android:id="@+id/editText" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
/> 
<Button 
android:id="@+id/button" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="Add+" 
/> 
관련 문제