有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在AlertDialogue上显示复选框的文本

选中时,我想在AlertDialogue(例如“Sports”)上显示复选框中的文本,以及姓名、电子邮件和出生日期消息。设置姓名、电子邮件和日期后,AlertDialogue将显示我输入或设置的内容。我希望它也显示我选中的复选框

package com.example.com.forminput; import 安卓.annotation.TargetApi; import 安卓.app.Activity; import 安卓.app.AlertDialog; import 安卓.content.DialogInterface; import 安卓.database.sqlite.SQLiteException; import 安卓.os.Build; import 安卓.os.Bundle; import 安卓.view.Menu; import 安卓.view.MenuInflater; import 安卓.view.MenuItem; import 安卓.view.View; import 安卓.view.View.OnClickListener; import 安卓.widget.Button; import 安卓.widget.CheckBox; import 安卓.widget.DatePicker; import 安卓.widget.EditText; import 安卓.widget.Toast; import 安卓.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:安卓="http://schemas.安卓.com/apk/res/安卓"

    安卓:layout_width="fill_parent"
    安卓:layout_height="wrap_content" >

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

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

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

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

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

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

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


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

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

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

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

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

    <Button
        安卓:id="@+id/ButtonClear"
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓: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) 个答案