簡體   English   中英

如何在操作欄中的“自定義對話框”中設置自定義按鈕單擊偵聽器?

[英]How to set a custom button click listener in a Custom Dialog from action bar?

好的,我無法弄清楚..這是我想要做的:

我在操作欄中有一個圖標-單擊該圖標時,會彈出一個包含自定義布局文件的對話框。 此布局具有一個EditText和一個Button 整個過程的目的是編寫電子郵件,然后在單擊時通過按鈕將其發送。 但是,當我單擊按鈕時,我得到了一個Nullpoint exception ..

這是一些代碼:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int itemId = item.getItemId();

    switch (itemId) {
        case R.id.action_logout:
            navigateToLogin();
            break;
        case R.id.action_edit_friends:
            Intent intent = new Intent(this, EditFriendsActivity.class);
            startActivity(intent);
            break;
        case R.id.action_message:
            Dialog messageDialog = new Dialog(this);
            messageDialog.setContentView(R.layout.message_bar);
            messageDialog.show();
            Button messageButton = (Button)messageDialog.findViewById(R.id.send_button);
            messageButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    EditText messageText = (EditText) findViewById(R.id.edit_text_message);
                    if (TextUtils.isEmpty(messageText.getText())) {
                        Toast.makeText(MainActivity.this, "Enter a message first!", Toast.LENGTH_SHORT).show();
                    } else {
                        String message = messageText.getText().toString();
                        Intent recipientsIntent = new Intent(MainActivity.this, RecipientsActivity.class);
                        recipientsIntent.putExtra("Text", message);
                        startActivity(recipientsIntent);
                    }
                }
            });
            break;
    }
    return super.onOptionsItemSelected(item);
}

我為對話框定制的布局文件就是這樣:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/structured_relative_interaction"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:gravity="bottom">

        <EditText
            android:id="@+id/edit_text_message"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_margin="5dp"
            android:layout_toLeftOf="@+id/send_button"
            android:gravity="bottom"
            android:hint="@string/send_message_hint"
            android:inputType="textCapSentences|textMultiLine"
            android:maxLines="15"
            android:padding="10dp" />

        <Button
            android:id="@+id/send_button"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_alignBottom="@id/edit_text_message"
            android:layout_alignParentRight="true"
            android:background="@drawable/ic_launcher"/>
    </RelativeLayout>
</RelativeLayout>

那我做錯了什么呢? 我無法解決這個問題,我查看了其他示例並閱讀了官方文檔中的示例,但是...真可惡。

Dialog還具有setContentView(View) ,因此您可以使視圖膨脹並將膨脹后的視圖傳遞給Dialog 您還可以使用展開視圖來找到實現onClickListener所需的視圖。 我的猜測是,除非在屏幕上可見,否則該視圖不屬於對話框的視圖層次結構。

所以我會說

View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.message_bar, null);
messageDialog.setContentView(view);
View messageButton = view.findViewById(R.id.send_button);
messageButton.setOnClickListener(...);

采用

EditText messageText = (EditText) messageDialog.findViewById(R.id.edit_text_message);

代替

EditText messageText = (EditText) findViewById(R.id.edit_text_message);

暫無
暫無

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

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