2016-11-27 56 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

你可能想考慮在回收站中添加一個textview。使得每次按下按鈕時都會添加新的卡片。 (這允許滾動行爲)。 –

回答

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+" 
/> 
相關問題