OnClick崩溃了我的应用程序

时间:2014-10-17 14:28:08

标签: android dialog onclick textview clickable

我在对话框中有一个textview,当我点击这个textview做某事时我想要 我想在点击textview时做点什么。

我在我的布局中添加了clickable和onClick 这是我的代码

public class Activitytwo extends ActionBarActivity {

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        //WORKING CODES
        //WORKING CODES
        //WORKING CODES
        //WORKING CODES
    }

    // This one to show the dialog
    //This one works fine!
    public void share(View view){
        sharedialog = new Dialog(Activitytwo.this);
        sharedialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        sharedialog.setContentView(R.layout.postdialog);
        sharedialog.setCanceledOnTouchOutside(true);
        sharedialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        sharedialog.show();

        }

    public void facebookp(View view){
        //This one for the TextView (onClick)
        //This one doesn't work..
    }

}

这是我的布局(postdialog.xml)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/postdialogl"
    android:layout_width="match_parent"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_height="150dp"
    android:background="@drawable/postdialog" >

    <TextView
        android:id="@+id/postdtitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="20dp"
        android:textSize="18sp"
        android:textStyle="bold"
        android:text="@string/post"
        android:textColor="#a7b8d6"/>

    <ImageView 
        android:id="@+id/poststrokea"
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="#202e4c"
        android:layout_below="@+id/postdtitle"
        android:layout_marginTop="5dp"/>
    <ImageView 
        android:id="@+id/poststrokeb"
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="#803e5482"
        android:layout_below="@+id/poststrokea"/>

    <TextView
        android:id="@+id/facebookpost"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
    android:layout_marginTop="60dp"
    android:layout_marginLeft="30dp"
    android:drawablePadding="5dp"
        android:text="@string/facebook"
        android:textColor="#a7b8d6"
        android:textSize="12sp"
        android:drawableTop="@drawable/facebook"
        android:clickable="true"
    android:onClick="facebookp"/>

</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

一种解决方案是在对话框中声明文本视图。

    public void share(View view){
      //your code.
        sharedialog = new Dialog(Activitytwo.this);
        sharedialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        sharedialog.setContentView(R.layout.postdialog);
        sharedialog.setCanceledOnTouchOutside(true); 
        sharedialog.getWindow().setBackgroundDrawable(newColorDrawable(android.graphics.Color.TRANSPARENT));

        final TextView tviPost = (TextView) sharedialog.findViewById(R.id.sharedialog);

     tviPost.setOnClickListener(new OnClickListener() {
                    public void onClick(View arg0) {
                     //Do something

                    }
        });

    sharedialog.show(); 
    }

并删除布局中的下一行。

android:clickable="true"
android:onClick="facebookp"