单击LinearLayout后,将显示带图像的警告对话框

时间:2014-03-05 23:38:24

标签: android

Symptoms.java

    package com.example.des;

    import java.util.ArrayList;

    import com.example.des.R;
    import com.example.des.Symptoms;
    import com.example.des.Learn_Dengue;

    import android.os.Bundle;
    import android.os.Handler;
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.content.Intent;
    import android.view.Gravity;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.CheckBox;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.Toast;

    public class Symptoms extends Activity implements OnClickListener{

        //set variables
        CheckBox q1; CheckBox q2; CheckBox q3;  CheckBox q4; CheckBox q5; CheckBox q6; 
        CheckBox q7; CheckBox q8; CheckBox q9 ;CheckBox q10; CheckBox q11; CheckBox q12;

        LinearLayout sas1; LinearLayout sas2; LinearLayout sas3; LinearLayout sas4; LinearLayout sas5; LinearLayout sas6; 
        LinearLayout sas7; LinearLayout sas8; LinearLayout sas9; LinearLayout sas10; LinearLayout sas11; LinearLayout sas12;

        Button btndone;
        Button btnlearn;

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

            q1 = (CheckBox)findViewById(R.id.q1a); //Question number 1--main symptom
            q2 = (CheckBox)findViewById(R.id.q2a);
            q3 = (CheckBox)findViewById(R.id.q3a);
            q4 = (CheckBox)findViewById(R.id.q4a);
            q5 = (CheckBox)findViewById(R.id.q5a);
            q6 = (CheckBox)findViewById(R.id.q6a);
            q7 = (CheckBox)findViewById(R.id.q7a);
            q8 = (CheckBox)findViewById(R.id.q8a);
            q9 = (CheckBox)findViewById(R.id.q9a);
            q10 = (CheckBox)findViewById(R.id.q10a);
            q11 = (CheckBox)findViewById(R.id.q11a);
            q12 = (CheckBox)findViewById(R.id.q12a);

            sas1 = (LinearLayout)findViewById(R.id.sas1view);
            sas2 = (LinearLayout)findViewById(R.id.sas2view);
            sas3 = (LinearLayout)findViewById(R.id.sas3view);
            sas4 = (LinearLayout)findViewById(R.id.sas4view);
            sas5 = (LinearLayout)findViewById(R.id.sas5view);
            sas6 = (LinearLayout)findViewById(R.id.sas6view);
            sas7 = (LinearLayout)findViewById(R.id.sas7view);
            sas8 = (LinearLayout)findViewById(R.id.sas8view);
            sas9 = (LinearLayout)findViewById(R.id.sas9view);
            sas10 = (LinearLayout)findViewById(R.id.sas10view);
            sas11 = (LinearLayout)findViewById(R.id.sas11view);
            sas12 = (LinearLayout)findViewById(R.id.sas12view);

            btndone = (Button) findViewById(R.id.done);
            btndone.setOnClickListener(this);   

            btnlearn = (Button) findViewById(R.id.learn);
            btnlearn.setOnClickListener(this);  
            }
        @Override

        public void onClick(View v) {  
            // When button "Result" is pressed
            switch (v.getId()) {
            case R.id.done:

            ArrayList<CheckBox> cbList = new ArrayList<CheckBox>();
            // Array CheckBox 2 - 12 inside an Array (sub symptoms)
            cbList.add((CheckBox)findViewById(R.id.q2a)); 
            cbList.add((CheckBox)findViewById(R.id.q3a));
            cbList.add((CheckBox)findViewById(R.id.q4a));
            cbList.add((CheckBox)findViewById(R.id.q5a));
            cbList.add((CheckBox)findViewById(R.id.q6a));
            cbList.add((CheckBox)findViewById(R.id.q7a));
            cbList.add((CheckBox)findViewById(R.id.q8a));
            cbList.add((CheckBox)findViewById(R.id.q9a));
            cbList.add((CheckBox)findViewById(R.id.q10a));
            cbList.add((CheckBox)findViewById(R.id.q11a));
            cbList.add((CheckBox)findViewById(R.id.q12a));

    // Starts Looping if CheckBox number 1 is selected and the others (result SUSPICIOUS)
            if (q1.isChecked()) {
                int count = 0;
                for (CheckBox cb : cbList) {
                    if (cb.isChecked()) {
                        count++;
                    }
                }           
                if (count == 2 || count == 1 || count == 0) {
                    new AlertDialog.Builder(this).setMessage(R.string.suspicious).show(); 
                }
            }

    // START Looping if CheckBox number 1 is selected but 11 and 12 are not (result MOST LIKELY)    
            if (q1.isChecked()) {

                int count = 0;
                for (CheckBox cb : cbList) {
                    if (cb.isChecked()) {
                        count++;
                    }
                }           
                if (count >= 3 && count < 11) {
                    new AlertDialog.Builder(this).setMessage(R.string.most_likely).show(); 
                }
            }

    // START Looping if all CheckBox are selected(result POSITIVE) 
            if (q1.isChecked() && q2.isChecked() && q3.isChecked() && q4.isChecked() && 
                q5.isChecked() && q6.isChecked() && q7.isChecked() && q8.isChecked() && 
                q9.isChecked() && q10.isChecked() && q11.isChecked()  && q12.isChecked()) {
                int count = 0;
                for (CheckBox cb : cbList) {
                    if (cb.isChecked()) {
                        count++;
                    }
                }           
                if (count == 11) {
                    new AlertDialog.Builder(this).setMessage(R.string.positive).show(); 
                }
            }

    // Starts Looping if CheckBox number 1 is not selected and the others are checked   
            if (!q1.isChecked()) {
                int count = 0;
                for (CheckBox cb : cbList) {
                    if (cb.isChecked()) {
                        count++;
                    }
                }
                if (count <= 1 || count > 1) {
                    //this Toast will show when only 1 or more check box will be checked(excluding check box #1)
                    /*Toast toast= Toast.makeText(getApplicationContext(), getString(R.string.negative), Toast.LENGTH_LONG);  
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.show();
                    */
                    new AlertDialog.Builder(this).setMessage(R.string.negative).show();
                }
            }
            break;          
            // When button "Learn More" is pressed
            case R.id.learn:
                Intent q = new Intent(this, Learn_Dengue.class);
                startActivity(q);
            break;
            } 

这里开始我的问题。

            sas1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//Alert Dialog with image           
                }
            });

            sas2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//Alert Dialog with image           
                }
            });

            sas3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//Alert Dialog with image           
                }
            });

            sas4.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//Alert Dialog with image           
                }
            });

            sas5.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//Alert Dialog with image           
                }
            });

            sas6.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//Alert Dialog with image           
                }
            });

            sas7.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//Alert Dialog with image           
                }
            });

            sas8.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//Alert Dialog with image           
                }
            });

            sas9.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//Alert Dialog with image           
                }
            });

            sas10.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//Alert Dialog with image           
                }
            });

            sas11.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//Alert Dialog with image           
                }
            });

            sas12.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//Alert Dialog with image           
                }
            });     
        }
    }

questions.xml

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/scrollView1"
        style="@style/styleName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/warning"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <CheckBox
                    android:id="@+id/q1a"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <LinearLayout 
                    android:layout_marginTop="15dp"
                    android:id="@+id/sas1view"
                    android:layout_width="match_parent"
                    android:layout_height="30dp" >

                    <TextView
                        android:id="@+id/sas1"
                        android:layout_width="wrap_content"
                        android:layout_height="30dp"
                        android:text="@string/sas1"
                        android:textAppearance="?android:attr/textAppearanceSmall"
                        android:textColor="#000000"
                        android:textSize="15sp"
                        android:textStyle="bold" />
                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <CheckBox
                    android:id="@+id/q2a"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <LinearLayout 
                    android:layout_marginTop="15dp"
                    android:id="@+id/sas2view"
                    android:layout_width="match_parent"
                    android:layout_height="30dp" >

                    <TextView
                        android:id="@+id/sas2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/sas2"
                        android:textAppearance="?android:attr/textAppearanceSmall"
                        android:textColor="#000000"
                        android:textSize="15sp"
                        android:textStyle="bold" />
                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <CheckBox
                    android:id="@+id/q3a"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <LinearLayout 
                    android:layout_marginTop="15dp"
                    android:id="@+id/sas3view"
                    android:layout_width="match_parent"
                    android:layout_height="30dp" >

                    <TextView
                        android:id="@+id/sas3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/sas3"
                        android:textAppearance="?android:attr/textAppearanceSmall"
                        android:textColor="#000000"
                        android:textSize="15sp"
                        android:textStyle="bold" />
                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <CheckBox
                    android:id="@+id/q4a"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <LinearLayout 
                    android:layout_marginTop="15dp"
                    android:id="@+id/sas4view"
                    android:layout_width="match_parent"
                    android:layout_height="30dp" >

                    <TextView
                        android:id="@+id/sas4"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/sas4"
                        android:textAppearance="?android:attr/textAppearanceSmall"
                        android:textColor="#000000"
                        android:textSize="15sp"
                        android:textStyle="bold" />
                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <CheckBox
                    android:id="@+id/q5a"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <LinearLayout 
                    android:layout_marginTop="15dp"
                    android:id="@+id/sas5view"
                    android:layout_width="match_parent"
                    android:layout_height="30dp" >

                    <TextView
                        android:id="@+id/sas5"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/sas5"
                        android:textAppearance="?android:attr/textAppearanceSmall"
                        android:textColor="#000000"
                        android:textSize="15sp"
                        android:textStyle="bold" />
                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <CheckBox
                    android:id="@+id/q6a"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <LinearLayout 
                    android:layout_marginTop="15dp"
                    android:id="@+id/sas6view"
                    android:layout_width="match_parent"
                    android:layout_height="30dp" >

                    <TextView
                        android:id="@+id/sas6"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/sas6"
                        android:textAppearance="?android:attr/textAppearanceSmall"
                        android:textColor="#000000"
                        android:textSize="15sp"
                        android:textStyle="bold" />
                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <CheckBox
                    android:id="@+id/q7a"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <LinearLayout 
                    android:layout_marginTop="15dp"
                    android:id="@+id/sas7view"
                    android:layout_width="match_parent"
                    android:layout_height="30dp" >

                    <TextView
                        android:id="@+id/sas7"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/sas7"
                        android:textAppearance="?android:attr/textAppearanceSmall"
                        android:textColor="#000000"
                        android:textSize="15sp"
                        android:textStyle="bold" />
                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <CheckBox
                    android:id="@+id/q8a"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <LinearLayout 
                    android:layout_marginTop="15dp"
                    android:id="@+id/sas8view"
                    android:layout_width="match_parent"
                    android:layout_height="30dp" >

                    <TextView
                        android:id="@+id/sas8"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/sas8"
                        android:textAppearance="?android:attr/textAppearanceSmall"
                        android:textColor="#000000"
                        android:textSize="15sp"
                        android:textStyle="bold" />
                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <CheckBox
                    android:id="@+id/q9a"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <LinearLayout 
                    android:layout_marginTop="15dp"
                    android:id="@+id/sas9view"
                    android:layout_width="match_parent"
                    android:layout_height="30dp" >

                    <TextView
                        android:id="@+id/sas9"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/sas9"
                        android:textAppearance="?android:attr/textAppearanceSmall"
                        android:textColor="#000000"
                        android:textSize="15sp"
                        android:textStyle="bold" />
                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <CheckBox
                    android:id="@+id/q10a"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <LinearLayout 
                    android:layout_marginTop="15dp"
                    android:id="@+id/sas10view"
                    android:layout_width="match_parent"
                    android:layout_height="30dp" >

                    <TextView
                        android:id="@+id/sas10"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/sas10"
                        android:textAppearance="?android:attr/textAppearanceSmall"
                        android:textColor="#000000"
                        android:textSize="15sp"
                        android:textStyle="bold" />
                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <CheckBox
                    android:id="@+id/q11a"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <LinearLayout 
                    android:layout_marginTop="15dp"
                    android:id="@+id/sas11view"
                    android:layout_width="match_parent"
                    android:layout_height="30dp" >

                    <TextView
                        android:id="@+id/sas11"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/sas11"
                        android:textAppearance="?android:attr/textAppearanceSmall"
                        android:textColor="#000000"
                        android:textSize="15sp"
                        android:textStyle="bold" />
                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <CheckBox
                    android:id="@+id/q12a"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <LinearLayout 
                    android:layout_marginTop="15dp"
                    android:id="@+id/sas12view"
                    android:layout_width="match_parent"
                    android:layout_height="30dp" >            

                    <TextView
                        android:id="@+id/sas12"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/sas12"
                        android:textAppearance="?android:attr/textAppearanceSmall"
                        android:textColor="#000000"
                        android:textSize="15sp"
                        android:textStyle="bold" />
                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="20dp" >

                <Button
                    android:id="@+id/done"
                    android:layout_width="120dp"
                    android:layout_height="40dp"
                    android:layout_marginLeft="35dp"
                    android:layout_marginTop="20dp"
                    android:background="@drawable/btn_orange"
                    android:text="@string/done"
                    android:textColor="#ffffff"
                    android:textSize="15sp" />

                <Button
                    android:id="@+id/learn"
                    android:layout_width="120dp"
                    android:layout_height="40dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="20dp"
                    android:background="@drawable/btn_black"
                    android:text="@string/learn"
                    android:textColor="#ffffff"
                    android:textSize="15sp" />
            </LinearLayout>
        </LinearLayout>

    </ScrollView>

这是我的完整代码,因为你请求了@mobie .. Symptoms.java和question.xml ..不管其他代码。只是在我问的警告对话框上。

1 个答案:

答案 0 :(得分:0)

为对话框创建如下所示的布局:dialog.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" >

<ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="5dp" />

<TextView
    android:id="@+id/text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="#FFF" 
    android:layout_toRightOf="@+id/image"/>

<Button
    android:id="@+id/dialogButtonOK"
    android:layout_width="100px"
    android:layout_height="wrap_content"
    android:text=" Ok "
    android:layout_marginTop="5dp"
    android:layout_marginRight="5dp"
    android:layout_below="@+id/image"
    />

</RelativeLayout>

然后,您要放置警报的位置,请执行以下操作

 final Dialog dialog = new Dialog(context);
        dialog.setContentView(R.layout.dialog);
        dialog.setTitle("Your Title");

        TextView text = (TextView) dialog.findViewById(R.id.text);
        text.setText("Text");
        ImageView image = (ImageView) dialog.findViewById(R.id.image);
        image.setImageResource(R.drawable.ic_launcher);

        Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
        dialogButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        dialog.show();