在AlertDialog中显示复选框的文本

时间:2015-03-12 16:13:11

标签: java android checkbox

我想在选中时从复选框中显示AlertDialogue上的文字(例如“体育”),以及姓名,电子邮件和出生日期信息。设置名称,电子邮件和日期后,“警报对话”将显示我输入或设置的内容。我希望它还显示我已选中的复选框

package com.example.com.forminput;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteException;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.CompoundButton;


public class DataInput extends Activity {


    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    public void onCreate(Bundle savedInstanceState) {


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

        final DatePicker dp = (DatePicker) this.findViewById(R.id.DatePickerDOB);
        dp.setCalendarViewShown(false);
        dp.init(1980, 5, 1, null); // set to 1st June 1980 - note months start at 0

        Button btnNext = (Button) findViewById(R.id.ButtonNext);
        Button btnClear = (Button) findViewById(R.id.ButtonClear);


		/* Handle the event generated when the user clicks the next button */
        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                displayNextAlert(); // call method defined later in the program
            }
        });

        btnClear.setOnClickListener(new View.OnClickListener() {
            @Override

                public void onClick(View v) {
                    ClearTextAlert(); // call method defined later in the program
                }
        });


    }


    private void displayNextAlert() {

        // Get what the user entered
        EditText nameInput = (EditText) findViewById(R.id.EditTextName);
        DatePicker dobInput = (DatePicker) findViewById(R.id.DatePickerDOB);
        EditText emailInput = (EditText) findViewById(R.id.EditTextEmail);

        String strName = nameInput.getText().toString();
        String strDOB = dobInput.getDayOfMonth() + "/"
                + (dobInput.getMonth() + 1) + "/" + dobInput.getYear();
        String strEmail = emailInput.getText().toString();


        // Create and display the Alert dialog
        new AlertDialog.Builder(this)
                .setTitle("Details entered")
                .setMessage(
                        " Details entered:\n " + strName + "\n " + strDOB
                                + "\n " + strEmail).setNeutralButton("Back",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // do nothing - it will just close when clicked
                    }
                }).show();
    }


    private void ClearTextAlert(){
        EditText textView;
        final EditText textView1 = (EditText) findViewById(R.id.EditTextName);
        final EditText textView2 = (EditText) findViewById(R.id.EditTextEmail);
        final DatePicker dp = (DatePicker) this.findViewById(R.id.DatePickerDOB);
        dp.init(1980, 5, 1, null); // set to 1st June 1980 - note months start at 0

        textView1.setText("");
        textView2.setText("");
        dp.init(1980, 5, 1, null);
        Toast t = Toast.makeText(getApplicationContext(),
                "Text Cleared",
                Toast.LENGTH_SHORT);
        t.show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_data_input, menu);
        return true;
    }

    /* Process the user's selection from the menu */
    public boolean onOptionsItemSelected(MenuItem item) {
        // Find which menu item was selected
        switch (item.getItemId()) { // item.getitemId() returns id of selected option
            case R.id.itemNext: // Next item was selected
                displayNextAlert(); // call the same method as for the Next Button
                return true;
            case R.id.itemExit: // Exit item was selected
                popupToast("You want to exit but why not just start using another application?");
                return true;

            case R.id.itemClear: // Clear item was selected
                ClearTextAlert();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    /* Utility method created to display a popup "toast" alert */
    private void popupToast(String message) {
        Toast.makeText(this, message, Toast.LENGTH_LONG).show();
    }
}
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/name_prompt"
        tools:context=".DataInput" />

    <EditText
        android:id="@+id/EditTextName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName" >
        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/question" />

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:text="@string/check_one" />

    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/checkBox1"
        android:layout_below="@+id/checkBox1"
        android:text="@string/check_two" />

    <CheckBox
        android:id="@+id/checkBox3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/checkBox2"
        android:text="@string/check_three" />


    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/DOB_prompt" />

    <DatePicker
        android:id="@+id/DatePickerDOB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/email_prompt" />

    <EditText
        android:id="@+id/EditTextEmail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textEmailAddress" />

    <Button
        android:id="@+id/ButtonNext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/next_command_prompt" />

    <Button
        android:id="@+id/ButtonClear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/clear_command_prompt" />

</LinearLayout>
</ScrollView>
<resources>

    <string name="app_name">FormInput</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_data_input">Enter your details</string>
    <string name="name_prompt">Name:</string>
    <string name="DOB_prompt">Date Of Birth:</string>
    <string name="email_prompt">Email:</string>
    <string name="next_command_prompt">Next</string>
    <string name="exit_command_label">Exit</string>
    <string name="clear_command_prompt">Clear</string>
    <string name="check_one">Sports</string>
    <string name="check_two">Reading</string>
    <string name="check_three">Sleeping</string>
    <string name="question">Hobbies:</string>

</resources>

0 个答案:

没有答案