猿问

自定义控件实现button功能,重写点击方法后安卓崩溃

public TitleLayout(Context context, AttributeSet attrs) {

super(context, attrs);

// 利用layouInflate.from(context).inflate(传入layout,父活动)

LayoutInflater.from(context).inflate(R.layout.title, this);

Button titleBack = (Button) findViewById(R.id.title_text);

Button titleEdit = (Button) findViewById(R.id.title_edit);

titleBack.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

( (Activity) getContext() ).finish();

}

});

titleEdit.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

Toast.makeText(getContext(), "You clicked Edit button", Toast.LENGTH_SHORT).show();

}

});

}

上面是自定义的Title,有一个title。xml在Layout中,

然后在activ_main.xml中用

    <com.example.helloworld.TitleLayout 

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        ></com.example.helloworld.TitleLayout>

安卓一打开就关闭,求救

慕仰2209588
浏览 2327回答 1
1回答

慕雪1579370

你把TitleLayout 的其他构造函数添加上去试试。
随时随地看视频慕课网APP

相关分类

Android
我要回答