ImageButton을 누르면 로그 메시지가 나타나는 테스트 응용 프로그램을 만들려고합니다. 단순한.
ImageButton보기가 클래스와 함께 작동하기를 원합니다.
나는 이렇게했습니다 : 올바른 이름으로 업데이트 되었습니다.
public class MainActivity extends Activity {
MyButtonClass btnOk = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
btnOk = new ButtonClass(this);
setContentView(R.layout.activity_main);
}
}
class MyButtonClass extends ImageButton{
public MyButtonClass(Context context) {
super(context);
findViewById(R.id.btButton);
OnClickListener oclBtnOk = new OnClickListener() {
@Override
public void onClick(View v) {
// change text of the TextView (tvOut)
Log.e("Log This:","Yay! I am working!");
}
};
setOnClickListener(oclBtnOk);
}
}
내 레이아웃 xml은 다음과 같습니다.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/btButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
</LinearLayout>
앱을 실행할 때 Log cat에 오류가 발생하지 않고 응용 프로그램이 종료되지 않지만 ImageButton을 눌렀을 때 아무 작업도 수행하지 않습니다.
왜 그렇게 엉망이 되었습니까? 코드를 많이 다듬어야합니다. 이 코드를 시도해 볼 수 있습니다
public class MyAndroidAppActivity extends Activity {
ImageButton imageButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
imageButton = (ImageButton) findViewById(R.id.imageButton1);
imageButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(MyAndroidAppActivity.this,
"ImageButton is clicked!", Toast.LENGTH_SHORT).show();
}
});
}
}
행운을 빕니다 =)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다