0

我正在尝试制作自定义对话框,以便在用户要选择时选择文本视图和图像。如果选择特定项目(以水平布局存在),如何在对话框中选中勾选或刻度线标记

在下面的代码中,我给出了三个文本视图和四个图像视图,希望如果用户要选择文本视图或图像视图,它应该是文本颜色变化,并且图像上应该出现刻度标记。

喜欢这个附加的图像。

attached image

按照附加图像我想使选择,如果用户选择的图像,应该显示为一个刻度标记,并且如果用户选择任何文本视图然后勾选下面的文本视图和颜色也应改变。

MainActivity

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     final Dialog dialog = new Dialog(this); 
     dialog.setContentView(R.layout.cust); 
     dialog.setTitle("Your title"); 
     dialog.setCancelable(false); 
     dialog.show(); 
     TextView d_btn_ok =  (TextView)  dialog.findViewById(R.id.btnOk); 
     TextView d_btn_cancel = (TextView) dialog.findViewById(R.id.btnCancel); 
     d_btn_ok.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       dialog.dismiss(); 
      } 
     }); 
     d_btn_cancel.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       dialog.dismiss(); 
      } 
     }); 

     TextView tvMsg11 =(TextView) dialog.findViewById(R.id.tvMsg11); 


     TextView tvMsg111 =(TextView) dialog.findViewById(R.id.tvMsg111); 
     TextView tvMsg1111 =(TextView) dialog.findViewById(R.id.tvMsg1111); 

     ImageView image1 = (ImageView) dialog.findViewById(R.id.image1); 
     image1.setImageDrawable(getResources().getDrawable(R.drawable.one)); 


     ImageView image2 = (ImageView) dialog.findViewById(R.id.image2); 
     image2.setImageDrawable(getResources().getDrawable(R.drawable.two)); 
     ImageView image3 = (ImageView) dialog.findViewById(R.id.image3); 
     image3.setImageDrawable(getResources().getDrawable(R.drawable.three)); 
     ImageView image4 = (ImageView) dialog.findViewById(R.id.image4); 
     image4.setImageDrawable(getResources().getDrawable(R.drawable.four)); 
    } 
} 

cust.xml

<?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="430dp" 
    android:orientation="vertical" 

    android:background="#ffffff" 
    android:theme="@style/AppTheme"> 

    <TextView 
     android:id="@+id/tvMsg" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="24dp" 
android:layout_marginLeft="20dp" 

     android:textSize="20sp" 
android:textColor="#293448" 
     android:text="Update your water intake" /> 
    <TextView 
     android:id="@+id/tvMsg1" 
     android:layout_below="@+id/tvMsg" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="25dp" 
     android:layout_marginLeft="21.5dp" 

     android:textSize="16sp" 
     android:textColor="#293448" 

     android:text="How many glasses of water do you 
drink daily?" /> 
    <LinearLayout 
     android:id="@+id/linearImage21" 

     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:layout_marginTop="20dp" 
     android:layout_below="@+id/tvMsg1" 
     android:weightSum="3"> 

     <TextView 
      android:id="@+id/tvMsg11" 
      android:layout_width="wrap_content" 
      android:layout_weight="1" 
      android:layout_marginLeft="23.5dp" 

      android:layout_height="wrap_content" 
      android:text="4 Glasses" 
      android:textSize="14sp"/> 


     <TextView 
      android:id="@+id/tvMsg111" 
      android:layout_toRightOf="@+id/tvMsg11" 
      android:layout_width="wrap_content" 
      android:layout_weight="1" 


      android:layout_height="wrap_content" 
      android:text="6 Glasses" 
      android:textSize="14sp"/> 



     <TextView 
      android:id="@+id/tvMsg1111" 
      android:layout_toRightOf="@+id/tvMsg111" 
      android:layout_width="wrap_content" 
      android:layout_weight="1" 


      android:layout_height="wrap_content" 
      android:text="8 Glasses" 
      android:textSize="14sp"/> 
     </LinearLayout> 
    <TextView 
     android:id="@+id/container" 
     android:layout_below="@+id/linearImage21" 
     android:layout_marginTop="48dp" 
     android:layout_marginLeft="21.5dp" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 

     android:textSize="16sp" 

     android:text="Your container size?" /> 

    <LinearLayout 
     android:id="@+id/linearImage1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:layout_marginTop="20dp" 
     android:layout_below="@+id/container" 
     android:weightSum="4"> 

     <ImageView 
      android:id="@+id/image1" 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:background="@drawable/custom_button_background" 

      android:checked="true" 
      android:layout_height="wrap_content" /> 

     <ImageView 
      android:id="@+id/image2" 
      android:layout_width="0dp" 
      android:layout_weight="1" 

      android:layout_height="wrap_content" /> 
     <ImageView 
      android:id="@+id/image3" 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:layout_height="wrap_content" /> 

     <ImageView 
      android:id="@+id/image4" 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:layout_height="wrap_content" /> 

    </LinearLayout> 



    <!-- <LinearLayout 
     android:id="@+id/linearImag2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:layout_marginTop="20dp" 
     android:layout_below="@+id/linearImage1" 
     android:weightSum="2"> 

     <ImageView 
      android:id="@+id/image3" 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:layout_height="wrap_content" /> 

     <ImageView 
      android:id="@+id/image4" 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:layout_height="wrap_content" /> 


    </LinearLayout>--> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/linearImage1" 
     android:orientation="horizontal" 
     android:layout_marginTop="28dp" 
     android:weightSum="2" 
    > 

     <TextView 
      android:id="@+id/btnOk" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="#fff" 
      android:layout_marginLeft="86.5dp" 
      android:textSize="16sp" 
      android:text="CANCEL" 
      android:textAllCaps="true" 
      android:textColor="#293448" /> 

     <TextView 
      android:id="@+id/btnCancel" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_marginLeft="48.5dp" 
      android:layout_weight="1" 
      android:textSize="16sp" 

android:layout_marginRight="6dp" 
      android:background="#fff" 
      android:text="SUBMIT" 
      android:textAllCaps="true" 
      android:textColor="#21c8d7" /> 

    </LinearLayout> 
</RelativeLayout> 

应用上面的代码后

我越来越喜欢这个形象

this image

回答

0

我们可以根据所附图像轻松完成

在需要文字视图或图像旁边的荧光笔时可见。 或在不需要或不需要时按下即可。就是这样。

为相同的工作编写了代码。

text_color.xml

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

     <item android:state_pressed="true" android:color="#00b200"></item> 
     <item android:state_focused="true" android:color="#00b200"/> 
     <item android:state_selected="true" android:color="#000000"/> 
     <item android:color="#d3d3d3"></item> 

    </selector> 

对于对话框:文本选择的基础上

文字的颜色变化 -

final Dialog dialog = new Dialog(this); 
     dialog.setContentView(R.layout.cust); 
     dialog.setTitle("Your title"); 
     dialog.setCancelable(false); 
     dialog.show(); 

     TextView d_btn_ok =  (TextView)  dialog.findViewById(R.id.btnOk); 
     TextView d_btn_cancel = (TextView) dialog.findViewById(R.id.btnCancel); 
     d_btn_ok.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       dialog.dismiss(); 
      } 
     }); 
     d_btn_cancel.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       dialog.dismiss(); 
      } 
     }); 
     final TextView tvMsg111 =(TextView) dialog.findViewById(R.id.tvMsg111); 
     final TextView tvMsg1111=(TextView) dialog.findViewById(R.id.tvMsg1111); 
     final TextView tvMsg11 = (TextView) dialog.findViewById(R.id.tvMsg11); 
     ImageView tick5=(ImageView) dialog.findViewById(R.id.tick5); 
     ImageView tick6=(ImageView) dialog.findViewById(R.id.tick6); 
     ImageView tick7=(ImageView) dialog.findViewById(R.id.tick7); 
     tvMsg11.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 

       // final ImageView imageView = (ImageView) dialog.findViewById(R.id.image2); 

       //tvMsg11.setImageDrawable(getResources().getDrawable(R.drawable.tick)); 
       tvMsg111.setSelected(false); 

       tvMsg1111.setSelected(false); 


       tvMsg11.setSelected(true); 

       tick5.setVisibility(View.VISIBLE); 
       tick6.setVisibility(View.INVISIBLE); 
       tick7.setVisibility(View.INVISIBLE); 

      } 
     }); 

     // final TextView tvMsg111 =(TextView) dialog.findViewById(R.id.tvMsg111); 
     tvMsg111.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 

       tvMsg11.setSelected(false); 
       tvMsg1111.setSelected(false); 
       tvMsg111.setSelected(true); 

       tick6.setVisibility(View.VISIBLE); 
       tick7.setVisibility(View.INVISIBLE); 
       tick5.setVisibility(View.INVISIBLE); 


      } 
     }); 

     //final TextView tvMsg1111 =(TextView) dialog.findViewById(R.id.tvMsg1111); 
     tvMsg1111.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 

       tvMsg11.setSelected(false); 
       tvMsg111.setSelected(false); 
       tvMsg1111.setSelected(true); 


       tick7.setVisibility(View.VISIBLE); 
       tick6.setVisibility(View.INVISIBLE); 
       tick5.setVisibility(View.INVISIBLE); 
      } 
     }); 




     /*TextView tvMsg111 =(TextView) dialog.findViewById(R.id.tvMsg111); 
     TextView tvMsg1111 =(TextView) dialog.findViewById(R.id.tvMsg1111);*/ 

     ImageView image1 = (ImageView) dialog.findViewById(R.id.image1); 
     ImageView tick1=(ImageView)dialog.findViewById(R.id.tick1); 
     ImageView tick2=(ImageView)dialog.findViewById(R.id.tick2); 
     ImageView tick3=(ImageView)dialog.findViewById(R.id.tick3); 
     ImageView tick4=(ImageView)dialog.findViewById(R.id.tick4); 
     image1.setImageDrawable(getResources().getDrawable(R.drawable.one)); 
     image1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       //final ImageView imageView = (ImageView) dialog.findViewById(R.id.image2); 

       tick1.setVisibility(View.VISIBLE); 
       tick2.setVisibility(View.INVISIBLE); 
       tick3.setVisibility(View.INVISIBLE); 
       tick4.setVisibility(View.INVISIBLE); 
       /* imageView.setImageDrawable(getResources().getDrawable(R.drawable.tick)); 
       imageView.setImageDrawable(getResources().getDrawable(R.drawable.two));*/ 
      } 
     }); 

     ImageView image2 = (ImageView) dialog.findViewById(R.id.image2); 
     // ImageView image21 = (ImageView) dialog.findViewById(R.drawable.four); 
// for replacing image on click of image 
     image2.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       //final ImageView imageView = (ImageView) dialog.findViewById(R.id.image2); 


       tick2.setVisibility(View.VISIBLE); 
       tick1.setVisibility(View.INVISIBLE); 
       tick3.setVisibility(View.INVISIBLE); 
       tick4.setVisibility(View.INVISIBLE); 
       /* imageView.setImageDrawable(getResources().getDrawable(R.drawable.tick)); 
       imageView.setImageDrawable(getResources().getDrawable(R.drawable.two));*/ 
      } 
     }); 
     image2.setImageDrawable(getResources().getDrawable(R.drawable.two)); 
     ImageView image3 = (ImageView) dialog.findViewById(R.id.image3); 
     image3.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       //final ImageView imageView = (ImageView) dialog.findViewById(R.id.image2); 


       tick3.setVisibility(View.VISIBLE); 
       tick1.setVisibility(View.INVISIBLE); 
       tick2.setVisibility(View.INVISIBLE); 
       tick4.setVisibility(View.INVISIBLE); 

       /* imageView.setImageDrawable(getResources().getDrawable(R.drawable.tick)); 
       imageView.setImageDrawable(getResources().getDrawable(R.drawable.two));*/ 
      } 
     }); 

     image3.setImageDrawable(getResources().getDrawable(R.drawable.three)); 
     ImageView image4 = (ImageView) dialog.findViewById(R.id.image4); 
     image4.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       //final ImageView imageView = (ImageView) dialog.findViewById(R.id.image2); 


       tick4.setVisibility(View.VISIBLE); 
       tick1.setVisibility(View.INVISIBLE); 
       tick2.setVisibility(View.INVISIBLE); 
       tick3.setVisibility(View.INVISIBLE); 
       /* imageView.setImageDrawable(getResources().getDrawable(R.drawable.tick)); 
       imageView.setImageDrawable(getResources().getDrawable(R.drawable.two));*/ 
      } 
     }); 

     image4.setImageDrawable(getResources().getDrawable(R.drawable.four)); 

cust.xml

<?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="430dp" 
     android:orientation="vertical" 

     android:background="#ffffff" 
     android:theme="@style/AppTheme"> 

     <TextView 
      android:id="@+id/tvMsg" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="24dp" 
    android:layout_marginLeft="20dp" 

      android:textSize="20sp" 
    android:textColor="#293448" 
      android:text="Update your water intake" /> 
     <TextView 
      android:id="@+id/tvMsg1" 
      android:layout_below="@+id/tvMsg" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="25dp" 
      android:layout_marginLeft="21.5dp" 

      android:textSize="16sp" 
      android:textColor="#293448" 

      android:text="How many glasses of water do you 
    drink daily?" /> 
     <LinearLayout 
      android:id="@+id/linearImage21" 

      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:layout_marginTop="20dp" 
      android:layout_below="@+id/tvMsg1" 
      android:weightSum="3"> 

      <TextView 
       android:id="@+id/tvMsg11" 
       android:layout_width="wrap_content" 
       android:layout_weight="1" 
       android:layout_marginLeft="23.5dp" 
       android:textColor="@drawable/text_color" 
       android:layout_height="wrap_content" 
       android:text="4 Glasses" 

       android:textSize="14sp"/> 


      <TextView 
       android:id="@+id/tvMsg111" 
       android:layout_toRightOf="@+id/tvMsg11" 
       android:layout_width="wrap_content" 
       android:layout_weight="1" 
       android:textColor="@drawable/text_color" 
       android:layout_marginLeft="8dp" 
       android:layout_height="wrap_content" 
       android:text="6 Glasses" 
       android:textSize="14sp"/> 



      <TextView 
       android:id="@+id/tvMsg1111" 
       android:layout_toRightOf="@+id/tvMsg111" 
       android:layout_width="wrap_content" 
       android:layout_weight="1" 
       android:layout_marginLeft="10dp" 
       android:textColor="@drawable/text_color" 
       android:layout_height="wrap_content" 
       android:text="8 Glasses" 
       android:textSize="14sp"/> 
      </LinearLayout> 

     <LinearLayout 
      android:id="@+id/lineartick" 

      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:layout_marginTop="2dp" 
      android:layout_below="@+id/linearImage21" 
      android:weightSum="3"> 

      <ImageView 
       android:id="@+id/tick5" 
       android:layout_width="16dp" 
       android:layout_weight="1" 
       android:src="@drawable/texthiglighter" 
       android:visibility="invisible" 
       android:layout_marginRight="12dp" 
       android:layout_height="10dp" /> 


      <ImageView 
       android:id="@+id/tick6" 
       android:layout_width="16dp" 
       android:layout_weight="0.7" 
       android:src="@drawable/texthiglighter" 
       android:visibility="invisible" 
       android:layout_marginLeft="1dp" 
       android:layout_toRightOf="@+id/image1" 
       android:layout_height="10dp" /> 

      <ImageView 
       android:id="@+id/tick7" 
       android:layout_width="16dp" 
       android:layout_weight="1.3" 
       android:src="@drawable/texthiglighter" 
       android:visibility="invisible" 

       android:layout_height="10dp" /> 

     </LinearLayout> 

     <TextView 
      android:id="@+id/container" 
      android:layout_below="@+id/linearImage21" 
      android:layout_marginTop="48dp" 
      android:layout_marginLeft="21.5dp" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:textSize="16sp" 
      android:textColor="#293448" 
      android:alpha="80" 
      android:text="Your container size?" /> 

     <LinearLayout 
      android:id="@+id/linearImage1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:layout_marginTop="20dp" 
      android:layout_below="@+id/container" 
      android:weightSum="4"> 

      <ImageView 
       android:id="@+id/image1" 
       android:layout_width="0dp" 
       android:layout_weight="1" 
       android:checked="true" 
       android:layout_height="wrap_content" /> 
      <ImageView 
       android:id="@+id/tick1" 
       android:layout_width="13dp" 
       android:layout_weight="0.1" 
       android:src="@drawable/tick" 
       android:layout_marginLeft="0.1dp" 
       android:visibility="invisible" 
       android:layout_toRightOf="@+id/image1" 
       android:layout_height="13dp" /> 


      <ImageView 
       android:id="@+id/image2" 
       android:layout_width="0dp" 
       android:layout_weight="1" 

       android:layout_height="wrap_content" /> 
      <ImageView 
       android:id="@+id/tick2" 
       android:layout_width="13dp" 
       android:layout_weight="0.1" 
       android:src="@drawable/tick" 
       android:layout_marginLeft="0.1dp" 
       android:visibility="invisible" 
       android:layout_toRightOf="@+id/image2" 
       android:layout_height="13dp" /> 



      <ImageView 
       android:id="@+id/image3" 
       android:layout_width="0dp" 
       android:layout_weight="1" 
       android:layout_height="wrap_content" /> 

      <ImageView 
       android:id="@+id/tick3" 
       android:layout_width="13dp" 
       android:layout_weight="0.1" 
       android:src="@drawable/tick" 
       android:layout_marginLeft="0.1dp" 
       android:visibility="invisible" 
       android:layout_toRightOf="@+id/image3" 
       android:layout_height="13dp" /> 


      <ImageView 
       android:id="@+id/image4" 
       android:layout_width="0dp" 
       android:layout_weight="1" 

       android:layout_height="wrap_content" /> 
      <ImageView 
       android:id="@+id/tick4" 
       android:layout_width="13dp" 
       android:layout_weight="0.1" 
       android:src="@drawable/tick" 
       android:layout_marginLeft="0.1dp" 
       android:visibility="invisible" 
       android:layout_marginRight="25dp" 
       android:layout_toRightOf="@+id/image4" 
       android:layout_height="13dp" /> 


     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/linearscale" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:layout_marginTop="16dp" 
      android:layout_below="@+id/linearImage1" 
      android:weightSum="4"> 

      <TextView 
       android:id="@+id/onne" 
       android:layout_width="wrap_content" 
       android:layout_weight="1" 
       android:layout_marginLeft="5dp" 
       android:textColor="#283d65" 
       android:layout_height="wrap_content" 
       android:text="150 ML" 
       android:textAllCaps="true" 
       android:textSize="14sp"/> 



      <TextView 
       android:id="@+id/twoe" 
       android:layout_width="wrap_content" 
       android:layout_weight="1" 

       android:textColor="#283d65" 
       android:layout_height="wrap_content" 
       android:text="250 ML" 
       android:textAllCaps="true" 
       android:textSize="14sp"/> 

      <TextView 
       android:id="@+id/three" 
       android:layout_width="wrap_content" 
       android:layout_weight="1" 

       android:textColor="#283d65" 
       android:layout_height="wrap_content" 
       android:text="500 ML" 
       android:textAllCaps="true" 
       android:textSize="14sp"/> 

      <TextView 
       android:id="@+id/foure" 
       android:layout_width="wrap_content" 
       android:layout_weight="1" 
       android:layout_marginRight="6dp" 
       android:textColor="#283d65" 
       android:layout_height="wrap_content" 
       android:text="1 LTR" 
       android:textAllCaps="true" 
       android:textSize="14sp"/> 

    </LinearLayout> 

     <View 
      android:id="@+id/viewp" 
      android:layout_width="match_parent" 
      android:layout_height="1dp" 
      android:layout_marginLeft="6dp" 
      android:layout_marginRight="6dp" 
      android:layout_marginTop="10dp" 
      android:layout_gravity="center" 
      android:alpha="4" 
      android:background="#000000" 
      android:layout_below="@+id/linearscale"></View> 


     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/linearscale" 
      android:orientation="horizontal" 
      android:layout_marginTop="28dp" 
      android:weightSum="2" 
     > 

      <TextView 
       android:id="@+id/btnOk" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:background="#fff" 
       android:layout_marginLeft="86.5dp" 
       android:textSize="16sp" 
       android:text="CANCEL" 
       android:textAllCaps="true" 
       android:textColor="#293448" /> 

      <TextView 
       android:id="@+id/btnCancel" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_marginLeft="48.5dp" 
       android:layout_weight="1" 
       android:textSize="16sp" 

    android:layout_marginRight="6dp" 
       android:background="#fff" 
       android:text="SUBMIT" 
       android:textAllCaps="true" 
       android:textColor="#21c8d7" /> 

     </LinearLayout> 
    </RelativeLayout> 
相关问题