這是我如何使用LinearLayout爲其添加動態TextView到表格佈局的代碼。如何檢索TextView值,當我們觸摸它和TextView被添加到LinearLayout?
table_personalData.removeAllViews();
int i=0;
for(String header : headerDetail){
TextView label=new TextView(this);
TextView value=new TextView(this);
label.setTextAppearance(this, R.style.TextBase);
value.setTextAppearance(this, R.style.TextBase);
label.setText(String.format("%s:", header));
value.setText(String.format("%s", lead.data[i]==null?"":lead.data[i]));
i++;
LinearLayout rowview=new LinearLayout(this);
rowview.setOrientation(0);
rowview.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
rowview.addView(label);
rowview.addView(value);
table_personalData.addView(rowview);
}
通過上面的代碼,我得到了我的表格佈局動態添加textview。 這裏我使用班輪佈局在一行中添加兩個文本視圖。
headerDetail is a ArrayList<String> headerDetail
lead.data[i] i used this to get my values for particular label.
所有這些工作對我很好我的問題是什麼現在 現在我想做的一件事是,當我們點擊我的LinearLayout的TextView中的一個我想,在TextView的動態。 因此,最後我的問題是,當我們觸摸或點擊Linearlayout中的任何textview並且LinearLayout在TableLayout中時,我們的問題即將得到檢索textview值。
任何人都知道我該怎麼處理這件事。 我也嘗試下面的東西,但我不明白我可以從下面的代碼中獲取TextView值。
table_personalData.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
for(int i=0;i<table_personalData.getChildCount();i++){
//CharSequence desc=table_personalData.getChildAt(i).getContentDescription();
//Log.v("log_tag", "the values are"+ desc);
}
}
});
編輯:
,如果你想的TextView的價值,當我們觸摸它在沒有我寫下面兩個方式:
value.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
TextView txt;
txt=(TextView)findViewById(v.getId());
String tempEmail;
tempEmail=txt.getText().toString().trim();
String aEmailList[]={tempEmail};
pattern=Pattern.compile(EMAIL_PATTERN);
if(validate(tempEmail)){
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,aEmailList);
startActivity(emailIntent);
}else{
pattern=Pattern.compile(PHONE_No_PATTERN);
if(validate(tempEmail)){
tempEmail=tempEmail.replaceAll("[^\\d]", "");
String uri="tel:" +tempEmail;
Intent mIntent=new Intent(Intent.ACTION_CALL);
mIntent.setData(Uri.parse(uri));
startActivity(mIntent);
}else{
//Toast.makeText(LeadDetailActivity.this, "Please Touch on Email or Mo:Number", Toast.LENGTH_SHORT).show();
}
}
}
});
感謝響應,但我不能這樣做,因爲所有的TextView是一個的LinearLayout和LinearLayout中的孩子是父母根的TabeTayout孩子所以,當我嘗試用你的代碼我clickListener不found.and一個更這個for循環是一個功能。 – Herry
好吧,我得到了我的解決方案非常感謝你。 – Herry
@Herry請發佈解決方案。 – panchicore