如何设置Crouton Toast内按钮的监听器并使用该按钮清除Crouton Toast?

时间:2013-03-07 05:23:30

标签: android crouton

我有一个应用程序,在操作栏下方显示吐司。我正在使用Crouton库来显示吐司。在这里,我为toast放置自定义布局,该布局包含一个textview和一个按钮(用于关闭选项)。所以Toast出现了按钮。如果我点击它应该关闭吐司但没有任何反应..下面这个例子的代码。感谢任何帮助

MainActivity.java

public class MainActivity extends SherlockActivity implements OnClickListener {

private View customToastView;
private TextView customToastMessageView;
private Button customToastCloseButton;
private Button doneButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    doneButton = (Button) findViewById(R.id.done_button);
    customToastView = getLayoutInflater().inflate(R.layout.toast_custom_layout, null);
    customToastMessageView = (TextView) customToastView.findViewById(R.id.messages);
    customToastCloseButton = (Button) customToastView.findViewById(R.id.close_button);
    customToastCloseButton.setOnClickListener(this);
    doneButton.setOnClickListener(this);
}

@Override
public void onClick(View v) {

    switch (v.getId()) {
        case R.id.done_button:
            Crouton.cancelAllCroutons();
            customToastMessageView.setText("Message");
            Crouton.show(this, customToastView);
            break;
        case R.id.close_button:
            Crouton.cancelAllCroutons();
            break;
      }
  }
}

toast_custom_layout.xml

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

    <TextView
        android:id="@+id/messages"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="26dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/close_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="23dp"
        android:text="Button" />

</RelativeLayout>

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<Button
    android:id="@+id/done_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="146dp"
    android:text="Button" />

</RelativeLayout>

0 个答案:

没有答案