I am newbie to Android, apology if my question is so stupid!
I created a new TextView in a public class, but then when I wanted to assign ID to it, [ I tried text1.setID(1) ], but Eclipse not recognize text1.
What is the problem? Did I define TextView wrong?
Actually my goal is that to create a class (here Class Post) which include 2 textViews(text1 & text2), then I want to create object from this class in my program( ex. in main activity), is this a right way to do it? ( a kind of simply creating new android widget)
public class Post{
Context Creator_Context;
public Post(Context context)
{
ctx= context;
}
//Creating a textview.
TextView text1 = new TextView(Ctx);
TextView text2 = new TextView(Ctx);
///////here is the PROBLEM////// :
text1.setID(1);
}
Thanks,
You can try setting tag for the textView that you have created. (Tag can be an alphanumeric string)
textView.setTag("TAG-1");
This is not the way you need to follow to create a simple widget.
Instead of that, a more simple way would be to extend an existing component and to put your textview inside. For instance you could extend FrameLayout :
public class TwoLinesTextButton extends FrameLayout {
private TextView textView1;
private TextView textView2;
public TwoLinesTextButton(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context, attrs);
}
public TwoLinesTextButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView(context, attrs);
}
public void fill(String text1, String text2) {
textView1.setText(text1);
textView2.setText(text2);
}
private void initView(Context context, AttributeSet attrs) {
LayoutInflater infl = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = infl.inflate(getLayoutResource(), null);
textView1 = (TextView) view.findViewById(R.id.text_1);
textView2 = (TextView) view.findViewById(R.id.text_2);
this.addView(view);
}
protected int getLayoutResource() {
return R.layout.twolinestextbutton;
}
}
and in layout/twolinestextbutton.xml :
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="@+id/text_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="250dp"/>
<TextView
android:id="@+id/text_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="250dp"/>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.