简体   繁体   中英

How to add setOnClickListener for each TextView in the custom Layout

//custom_dialog.xml

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

    <TextView
        android:id="@+id/first_box"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="45dp"
        android:layout_marginLeft="10dp"
        android:background="@color/action_bar_color_first"
        />

    <TextView
        android:id="@+id/second_box"
        android:layout_toRightOf="@id/first_box"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="45dp"
        android:background="@color/action_bar_color_second"
        android:layout_marginLeft="10dp"/>

    <TextView
        android:id="@+id/third_box"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="45dp"
        android:layout_below="@+id/first_box"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:background="@color/action_bar_color_third"/>

    <TextView
        android:id="@+id/fourth_box"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="45dp"
        android:layout_toRightOf="@+id/third_box"
        android:layout_below="@+id/second_box"
        android:background="@color/action_bar_color_fourth"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"/>

    <TextView
        android:id="@+id/fifth_box"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="45dp"
        android:layout_toRightOf="@+id/second_box"
        android:background="@color/action_bar_color_fifth"
        android:layout_marginLeft="10dp"/>

    <TextView
        android:id="@+id/sixth_box"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="45dp"
        android:layout_toRightOf="@+id/fourth_box"
        android:layout_below="@+id/fifth_box"
        android:background="@color/action_bar_color_sixth"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"/>


</RelativeLayout>

//CustomDialog.java
package com.example.android.myapplication;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

/**
 * Created by Home on 2/24/2017.
 */
public class CustomDialog extends DialogFragment {

    LayoutInflater inflater;
    View v;

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        inflater = getActivity().getLayoutInflater();
        v = inflater.inflate(R.layout.custom_dialog, null);
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setView(v);

        return builder.create();

    }

}

What I want is to have a listener on all of the text views inside the DialogBox so that whenever user presses any one of these views that textview's color will be implied. Like in a note app there is a option to select theme color.

I am unable to do that. Please suggest me the right way to do this.

you can simply set the click listener like this..

((TextView)v.findViewById(R.id.first_box)).setOnClick....

and so on..

I prefer to create a new listener for each view. Most of the time I create anonymous classes for this purpose, but every once in a while a named class is appropriate, for example when multiple views have similar behavior. Here is an example:

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstance) {
        setContentView(R.layout.custom_dialog);
        TextView first = findViewById(R.id.first_box);
        first.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                // logic goes here
            }
        });
    }
}

Rinse and repeat for each view.

Note that I used an Activity in my example code, but the same principle applies for Fragments.

Also, I suggest that you use more descriptive names than just numbers. If it makes sense to use numbers in your names, like in a calculator app, then you should probably have an array of Views in your code. You might even take it a step further and create the Views programmatically to take advantage of for loops.

Since you already inflated the custom dialog, follow the below steps

1) Get a reference to each textview.

2) Create a listener for each view

3) Change the theme when it is clicked.

// Initialize First Text View
TextView firstBoxTextView = (TextView) v.findViewById(R.id.first_box);
// Set click listener for the First Text View
firstBoxTextView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            // Change the theme
        }
    });

Do this for the rest of the text views.

Your code should look something like this, make necessary changes.

public class CustomDialog extends DialogFragment {
LayoutInflater inflater;
View v;
TextView text1;
TextView text2;
TextView text3;
TextView text4;
TextView text5;
TextView text6;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu);
    addListenerOnTextView();

}

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    inflater = getActivity().getLayoutInflater();
    v = inflater.inflate(R.layout.custom_dialog, null);
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setView(v);

    return builder.create();
}



public void addListenerOnTextView() {

    final Context context = this;

    text1 = (TextView) findViewById(R.id.first_box);

    text1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            //Your code for what you want to do goes here
            finish();
        }

    });

}

}

repeat the public void addListenerOnTextView() { constructor for text 2 to text 6

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM