안드로이드 ScrollView 내부의 Button OnClickListener 질문
조회수 2150회
안드로이드 ScrollView 내부의 Button들에 OnClickListner를 만드려고 View.OnClickListener를 implements 하였지만 어째서인지 잘 작동하지 않는 듯 합니다. 어떤 것이 문제인가요...? Scroll View 내부에 Button이 있을 경우 무언가 다른 방법이 필요한가요...? 알려주시면 감사하겠습니다.
MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private WebView mWebView;
private EditText searchBox;
private Button naver;
private Button daum;
private Button google;
private Button namuwiki;
private Button naver_endic;
private ImageButton mic;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
naver = (Button) findViewById(btn_naver);
daum = (Button) findViewById(R.id.btn_daum);
google = (Button) findViewById(R.id.btn_google);
namuwiki = (Button) findViewById(R.id.btn_namuwiki);
naver_endic = (Button) findViewById(R.id.btn_naver_endic);
mic = (ImageButton) findViewById(R.id.btn_mic);
searchBox = (EditText) findViewById(R.id.searchBox);
mWebView = (WebView) findViewById(R.id.webview_content);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient());
mWebView.loadUrl("http://www.taila.tk/aiosearch/");
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_naver:
mWebView.loadUrl("http://www.naver.com");
Toast.makeText(MainActivity.this, "네이버", Toast.LENGTH_SHORT).show();
break;
case R.id.btn_daum:
Toast.makeText(MainActivity.this, "다음", Toast.LENGTH_SHORT).show();
break;
case R.id.btn_google:
Toast.makeText(MainActivity.this, "구글", Toast.LENGTH_SHORT).show();
break;
case R.id.btn_namuwiki:
Toast.makeText(MainActivity.this, "나무위키", Toast.LENGTH_SHORT).show();
break;
case R.id.btn_naver_endic:
Toast.makeText(MainActivity.this, "네이버 영어사전", Toast.LENGTH_SHORT).show();
break;
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
tools:context="tk.taila.aiosearch.MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/border"
android:layout_marginBottom="8dp"
android:padding="4dp" >
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
android:layout_weight="1">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Button
android:text="N"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_naver"
android:layout_weight="1" />
<Button
android:text="D"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_daum"
android:layout_weight="1" />
<Button
android:text="G"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_google"
android:layout_weight="1" />
<Button
android:text="NW"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_namuwiki"
android:layout_weight="1" />
<Button
android:text="NE"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_naver_endic"
android:layout_weight="1" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</LinearLayout>
(activity_main.xml에서 불필요한 부분은 조금 삭제하였습니다)
++ xml에서 onClick을 지정하였을 경우에는 문제 없이 작동 하였습니다. 이 방법으로 OnClickListener를 만들었을 때만 문제가 발생하네요. 어느 것이 문제인지 알려주시면 감사하겠습니다...!
-
(•́ ✖ •̀)
알 수 없는 사용자
1 답변
-
naver = (Button) findViewById(btn_naver); naver.setOnClickListener(this);
OnClickListener 인터페이스를 통해 구현하였으므로, 위와 같은 방법으로 리스너 연결을 해주어야합니다.
이벤트 리스너 연결하는 5가지 방법 입니다. 참고하시면 좋을 것 같네요.
댓글 입력