onclick事件未被触发

时间:2013-08-07 12:21:19

标签: android

这是我的代码,你能告诉我这是什么问题吗?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/background_bf"
    android:orientation="vertical" >

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_margin="10dp"
        android:layout_weight="1"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            android:orientation="vertical" >

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="40dp"
                android:gravity="center_vertical"
                android:padding="10dp"
                android:text="SELECT YOUR LANGUAGES"
                android:textColor="#8E8D8D"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/langDutch"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:drawableLeft="@drawable/bg_settings_dutchon_hc_2x"
                android:drawablePadding="10dp"
                android:gravity="center_vertical"
                android:onClick="onlanguageSelect"
                android:padding="10dp"
                android:text="Dutch"
                android:textColor="#454C52" />

            <TextView
                android:id="@+id/langFlemish"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:drawableLeft="@drawable/bg_settings_dutchact_hc_2x"
                android:drawablePadding="10dp"
                android:gravity="center_vertical"
                android:onClick="onlanguageSelect"
                android:padding="10dp"
                android:text="Flemish"
                android:textColor="#454C52" />

            <TextView
                android:id="@+id/langFrench"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:drawableLeft="@drawable/bg_settings_franceon_hc_2x"
                android:drawablePadding="10dp"
                android:gravity="center_vertical"
                android:onClick="onlanguageSelect"
                android:padding="10dp"
                android:text="French"
                android:textColor="#454C52" />
        </LinearLayout>
    </ScrollView>

    <TextView
        android:id="@+id/settingscontinue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_margin="20dp"
        android:background="@drawable/button_hc_bf_language_2x"
        android:gravity="center"
        android:text="CONTINUE"
        android:textColor="@color/white"
        android:textSize="16sp"
        android:textStyle="bold" />

</LinearLayout>

这是活动:

public void onlanguageSelect(View v){
        Log.v("Language Settings: ", " onlanguageSelect clicked.");
        int id = v.getId();
        String lang;
        switch (id) {
        case R.id.langDutch:
            lang = "nl_NL";
            break;
        case R.id.langFlemish:
            lang = "nl_BE";
            break;
        case R.id.langFrench:
            lang = "fr";
            break;
        default:
            lang = "nl";
            break;
        }
        changeLocale(lang);
    }

    private void changeLocale(String localeString){
        Locale locale = new Locale(localeString);
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
        Log.v("Language Settings: ", "language changed successfully.");
    }

2 个答案:

答案 0 :(得分:1)

正如Raghunandan所说,每个View都必须被标记为可点击以使onClick处理程序工作。

尝试使用TextViews:

        <TextView
            android:id="@+id/langFrench"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:drawableLeft="@drawable/bg_settings_franceon_hc_2x"
            android:drawablePadding="10dp"
            android:gravity="center_vertical"
            android:onClick="onlanguageSelect"

            android:clickable="true"

            android:padding="10dp"
            android:text="French"
            android:textColor="#454C52" />

答案 1 :(得分:0)

确保您已在活动类中实现了onClickListner()接口。 并确保在onCreate()方法中使用如下界面注册所有TextView。

TextView btnDutch = (TextView ) findViewById(R.id.langDutch);
btnDutch.setOnClickListener(this);

这应该有效。