Button OnClickListener question inside Android ScrollView

Asked 2 years ago, Updated 2 years ago, 46 views

View to create an OnClickListner on Button inside Android ScrollView.I have implemented OnClickListener, but somehow it doesn't seem to work well. What is the problem...? If there is a Button inside the Scroll View, is there any other way...? I'd appreciate it if you let me know.

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, "Naver", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_daum:
                Toast.makeText (MainActivity.this, "Next", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_google:
                Toast.makeText (MainActivity.this, "Google", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_namuwiki:
                Toast.makeText (MainActivity.this, "Treewiki", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_naver_endic:
                Toast.makeText (MainActivity.this, "Naver English Dictionary", 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>

(I deleted some unnecessary parts from activity_main.xml)

If onClick was specified in ++ xml, it worked without any problems. The problem only occurs when you create OnClickListener with this method. I would appreciate it if you could tell me which is the problem...!

android

2022-09-22 20:12

1 Answers

naver = (Button) findViewById(btn_naver);
naver.setOnClickListener(this);

Implemented through the OnClickListener interface You need to connect to the listener in the same way as above.

Five ways to connect event listeners. I think it would be good to refer to.


2022-09-22 20:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.