簡體   English   中英

button.setOnclickListener(this)有問題

[英]Problem with button.setOnclickListener(this)

我開始學習android,正在嘗試進行登錄活動,但由於任何原因我的按鈕均無法使用。

我在課堂上實現了view.OnClickListener

這是我的Java代碼:

public class Login extends AppCompatActivity implements View.OnClickListener{

    private Button btn_login, btn_reset;
    private EditText et_username, et_pwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        btn_login = (Button) findViewById(R.id.bt_login_login);
        btn_login.setOnClickListener(this);

        btn_reset = (Button) findViewById(R.id.bt_log_reset);
        btn_reset.setOnClickListener(this);

        et_username = (EditText) findViewById(R.id.et_log_username);
        et_pwd = (EditText) findViewById(R.id.et_log_password);

    }

    @Override
    public void onClick(View v) {

        Log.w("test","test");
        switch(v.getId()){
            case R.id.bt_login_login :
                DisplayData();
                break;

            case R.id.bt_log_reset :
                resetData();
                break;
        }
    }

我想知道這是否不是因為我的按鈕在這樣的樹中引起的:

activity_login.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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".Login">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/txt_username"/>

    <EditText
        android:id="@+id/et_log_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/txt_login"/>

    <EditText
        android:id="@+id/et_log_password"
        android:layout_width="match_parent"
        android:inputType="textPassword"
        android:hint="@string/pwd"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_horizontal|center_vertical"
        >
        <Button
            android:id="@+id/bt_login_login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/intro_login"/>


        <Button
            android:id="@+id/bt_log_reset"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/reset"/>

    </LinearLayout>

</LinearLayout>

附言:我英語說得不好,不便之處,敬請原諒。

確保您的活動implements View.OnClickListener其他所有內容似乎implements View.OnClickListener

您可以嘗試其他方法。 使用setOnClickListener時,可以傳遞OnClickListener的匿名類實現。

另一種方法是在xml中為每個按鈕定義一個onClick屬性,並為其指定一個函數名稱。 您必須在活動中擁有一個具有確切名稱的函數,才能觸發該函數(如果沒有正確的連接,Android Studio將不允許項目進行編譯。)

首選匿名類實現,因為您將無法始終在xml中聲明事件(例如,使用片段時)。

要在setOnclickListener中實現匿名類,請執行以下操作:

button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           //stuff to do when the button was clicked goes here. 
        }
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM