2016-11-27 23 views
0

私はAndroidのコーディングには新しく、ボタンを押したときにテキストビューを動的に作成する方法について質問があります。ボタンを押すたびに新しいテキストビューを追加するにはどうすればよいですか?

ボタンを押したときに別のアクティビティに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); 

    } 

^^^この部分は文字列が受信され、パラメータが設定され、カスタムテキストが新しいテキストビューに適用されます。

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のonClick(){ リターン新しい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+" 
/> 
関連する問題