-1
如何創建監聽器到這個文本視圖? 我嘗試創建自定義文本視圖,但當我將其註冊到新的偵聽器時,該程序會崩潰。添加新的監聽器到自定義文本視圖
這是擴展類和xml代碼的代碼。程序崩潰的行是粗體的。
public class CustomTextView extends TextView{
class Layout
{
public Layout()
{
thisTextView = (TextView)findViewById(R.id.thisTextView);
}
TextView thisTextView;
}
class Events
{
public Events()
{
l.thisTextView.
setOnClickListener(
new OnClickListener() { **// this is where the program collapse**
@Override
public void onClick(View v)
{
l.thisTextView.setText("Work!");
}
});
}
}
Layout l;
Events e;
Context ctx;
public ChordCustumeTextView(Context context)
{
super(context);
ctx = context;
l = new Layout();
e = new Events();
}
public void onClick(View v)
{
l.thisTextView.setText("Work!");
}
}
XML -
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/thisTextView"
android:clickable="true">
</TextView>
「程序崩潰」是什麼意思? – 2Dee 2015-01-21 10:07:33
layoutinflater用於初始化'thisTextView' – SorryForMyEnglish 2015-01-21 10:11:33
'inflater.inflate(R.layout.chord_custume_text_view,null)'試試這個 – SorryForMyEnglish 2015-01-21 10:32:54