点击cardview时,必须打开对话框

时间:2017-10-26 04:28:53

标签: java android android-cardview

activity_main.xml

cardview的

activity_main.xml表示显示卡片。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
card_view:cardCornerRadius="2dp"
android:id="@+id/c1"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:id="@+id/lin_nameage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    <TextView
        android:id="@+id/tvName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Name:"
        android:textColor="#000000"
        android:layout_marginStart="5dp"
        android:layout_marginLeft="5dp"
        android:gravity="start"
        android:textSize="15dp"/>

        <!-- to set edittext value -->
    <TextView
        android:id="@+id/tvet_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:layout_marginLeft="40dp"
        android:layout_toRightOf="@+id/tvName"
        android:textSize="15dp"/>
    <TextView
        android:id="@+id/tv_age"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="age:"
        android:textColor="#000000"
        android:layout_marginLeft="172dp"
        android:layout_toRightOf="@+id/tvet_name"
        android:textSize="15dp"/>

        <!-- to set edittext value -->
    <TextView
        android:id="@+id/tvet_age"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:layout_marginLeft="200dp"
        android:layout_toRightOf="@+id/tvet_name"
        android:textSize="15dp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/lin_nameage">
        <TextView
            android:id="@+id/tvrel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Relationship:"
            android:textColor="#000000"
            android:layout_marginStart="5dp"
            android:layout_marginLeft="5dp"
            android:gravity="start"
            android:textSize="15dp"/>


        <!-- to set edittext value -->
        <TextView
            android:id="@+id/tvet_rel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#000000"
            android:layout_marginLeft="40dp"
            android:layout_toRightOf="@+id/tvName"
            android:textSize="15dp"/>
        <TextView
            android:id="@+id/tv_occupation"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Occupation:"
            android:textColor="#000000"
            android:layout_marginLeft="80dp"
            android:layout_toRightOf="@+id/tvet_name"
            android:textSize="15dp"/>

        <!-- to set edittext value -->
        <TextView
            android:id="@+id/tvet_occu"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:textColor="#000000"
            android:layout_marginLeft="200dp"
            android:layout_toRightOf="@+id/tvet_name"
            android:textSize="15dp"/>
    </LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>

first.xml

first.xml是我点击cardview时必须打开的布局。

<?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"
android:paddingBottom="3dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="0dp">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/lin_act_name"
android:layout_height="60dp"
android:orientation="horizontal"
android:layout_marginTop="0dp">
<android.support.design.widget.TextInputLayout
    android:id="@+id/actname"
    android:layout_height="wrap_content"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:hint="Name"
    android:inputType="number">

    <EditText
        android:id="@+id/act_name"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</android.support.design.widget.TextInputLayout>

<android.support.design.widget.TextInputLayout
    android:id="@+id/actage"
    android:layout_height="wrap_content"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:hint="age">

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

</android.support.design.widget.TextInputLayout>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/lin_relationship"
android:layout_height="45dp"
android:layout_marginTop="0dp"
android:orientation="horizontal"
android:layout_below="@+id/lin_act_name">
<android.support.design.widget.TextInputLayout
android:id="@+id/actrelation"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:inputType="number"
android:hint="Relation">
<EditText
    android:id="@+id/act_relation"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/actoccupation"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:hint="Occupation">
<EditText
    android:id="@+id/act_occupation"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:inputType="number"/>

</android.support.design.widget.TextInputLayout>

</LinearLayout>
<Button
    android:id="@+id/okbtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="OK"
    android:layout_marginTop="20dp"
    android:layout_below="@+id/lin_relationship"
    android:layout_centerHorizontal="true"/>
</RelativeLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity implements 
OnClickListener {

final Context context = this;
private Button button;

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //button = (Button) findViewById(R.id.buttonShowCustomDialog);

    // add button listener
}

@Override
public void onClick(View view) {
    {
        if (view ==) {
            Details();
        }
    }

public void Details() {
    final Dialog dialog = new Dialog(context);
    dialog.setContentView(R.layout.first);
    dialog.setTitle("Enter Details");

    // set the custom dialog components - text, image and button
    EditText act_name = (EditText) dialog.findViewById(R.id.act_name);
    EditText act_age = (EditText) dialog.findViewById(R.id.act_age);
    EditText act_relation = (EditText) dialog.findViewById(R.id.act_relation);
    EditText act_occupation = (EditText) dialog.findViewById(R.id.act_occupation);
    //text.setText("Android custom dialog example!");
    //ImageView image = (ImageView) dialog.findViewById(R.id.image);
    //  image.setImageResource(R.drawable.ic_launcher);

    Button ok_btn = (Button) dialog.findViewById(R.id.okbtn);
    // if button is clicked, close the custom dialog
    ok_btn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });

    dialog.show();
}
}
}

当我点击cardview时,应该打开对话框。如何将Onclicklistener写入cardview。我没有使用任何Recyclerview。我很震惊。如何将Onclick写入cardview。 任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:2)

试试这个...在你的onCreate写

  CardView c1 = (CardView) findViewById(R.id.c1);
  c1 .setOnClickListener(this);

和点击方法

 @Override
public void onClick(View v) {

    switch (v.getId()) {

        case R.id.c1:
         //call your dialog box method here
           Details();
            break;

    }
}

对话框的方法

  AlertDialog alert;
// Alert dialog box method
private void Details(){
    AlertDialog.Builder builder = new AlertDialog.Builder(this,R.style.MyAlertDialogStyle);

    builder.setMessage("Please subscribe to use the premium version")
            .setCancelable(false)
            .setPositiveButton( getResources().getString(R.string.ok), new DialogInterface.OnClickListener()
            {
                public void onClick(final DialogInterface dialog, int id) {

                }
            }
            );


    //dialog box details
    alert = builder.create();
    alert.setTitle("Enter Details");
    alert.show();
}

如果您没有主题

,请在style.xml文件中设置此主题
    <style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
    <!-- Used for the buttons -->
    <item name="colorAccent">#FFFFFF</item>
    <!-- Used for the title and text -->
    <item name="android:textColorPrimary">#FFFFFF</item>
    <!-- Used for the background -->
    <item name="android:background">@color/colorPrimaryDark</item>
</style>

答案 1 :(得分:0)

将此添加到您的活动onCreate。

findViewById(R.id.c1).setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View view) {
        // Open Dialog here.
    }
});

答案 2 :(得分:0)

试试这个

public class MainActivity extends AppCompatActivity implements 
OnClickListener {

final Context context = this;
private CardView mcardView;

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mcardView = (CardView) findViewById(R.id.c1);

    mcardView.setOnClickListner(this);
}

@Override
public void onClick(View view) 
    {
        if (view == mcardView) {
            Details();
        }
    }

public void Details() {
    final Dialog dialog = new Dialog(context);
    dialog.setContentView(R.layout.first);
    dialog.setTitle("Enter Details");

    // set the custom dialog components - text, image and button
    EditText act_name = (EditText) dialog.findViewById(R.id.act_name);
    EditText act_age = (EditText) dialog.findViewById(R.id.act_age);
    EditText act_relation = (EditText) dialog.findViewById(R.id.act_relation);
    EditText act_occupation = (EditText) dialog.findViewById(R.id.act_occupation);
    //text.setText("Android custom dialog example!");
    //ImageView image = (ImageView) dialog.findViewById(R.id.image);
    //  image.setImageResource(R.drawable.ic_launcher);

    Button ok_btn = (Button) dialog.findViewById(R.id.okbtn);
    // if button is clicked, close the custom dialog
    ok_btn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });

    dialog.show();
}
}