1

我有一个应用程序,它在操作栏下方显示吐司。我正在使用 Crouton 库来显示吐司。在这里,我为 toast 放置自定义布局,该布局包含一个 textview 和一个按钮(用于关闭选项)。所以 Toast 出现了按钮。如果我单击它应该关闭 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>
4

0 回答 0