我对Android编码很新,我有一个关于按下按钮时动态创建textview的问题。
我已经想出了如何在按下按钮时将文本视图添加到另一个活动,但我想在每次按下按钮时都这样做我想每次添加新的文本视图,而不是删除之前创建的旧文本视图。 / p>
viewWillUnload
^^^这部分是接收字符串的地方,设置了参数并且自定义字体应用于新的文本视图。
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);
}
^^^这是发送字符串的部分。
答案 0 :(得分:1)
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");
}
private 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+"
/>