"Android" Android in Alertdialog use Daquan

Source: Internet
Author: User

Package Com.ceac.deng;


Import Android. r.string;
Import android.support.v7.app.ActionBarActivity;
Import Android.app.AlertDialog;
Import Android.app.AlertDialog.Builder;
Import Android.content.DialogInterface;
Import Android.os.Bundle;
Import Android.os.SystemClock;
Import Android.view.Menu;
Import Android.view.MenuItem;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.Chronometer;
Import Android.widget.Toast;
Import Android.widget.Chronometer.OnChronometerTickListener;




public class Mainactivity extends Actionbaractivity {


Private chronometer chronometer;
Private Button Stopbutton;
Private Button Startbutton;

Private button button;
Private Button button2;
Private Button Button3;
Private Button Button4;

boolean[] CheckedItems = new boolean[] {false, True, False, True, False, true};
string[] items = new string[] {"Plants vs. Zombies", "Angry Birds", "Bubble Dragon", "Happy Farm", "QQ rook", "Super Marie"};

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);

Findview ();

Chronometer.setbase (Systemclock.elapsedrealtime ());
Chronometer.setformat ("How much time has been spent:%s");
Chronometer.start ();

Chronometer.setonchronometerticklistener (New Onchronometerticklistener () {

@Override
public void Onchronometertick (chronometer chronometer) {
TODO auto-generated Method Stub
if (Systemclock.elapsedrealtime ()-chronometer.getbase () >= 100000)
{
Chronometer.stop ();
}
}
});

Setlistenner ();
}


/**
* Findview
*/
private void Findview ()
{
Chronometer = (chronometer) Findviewbyid (r.id.chronometer1);
Stopbutton = (Button) Findviewbyid (r.id.stop);
Startbutton = (Button) Findviewbyid (R.id.start);

Button = (button) Findviewbyid (R.id.button1);
Button2 = (Button) Findviewbyid (R.id.button2);
Button3 = (Button) Findviewbyid (R.id.button3);
Button4 = (Button) Findviewbyid (R.ID.BUTTON4);


}

private void Setlistenner ()
{
Stopbutton.setonclicklistener (New Onclicklistener () {

@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Chronometer.stop ();
System.out.println ("Stop chronometer ....");
}
});

Startbutton.setonclicklistener (New Onclicklistener () {

@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Chronometer.setbase (Systemclock.elapsedrealtime ());
Chronometer.start ();
System.out.println ("Start chronometer ....");
}
});

Button.setonclicklistener (New Onclicklistener () {

@Override
public void OnClick (View v) {


Showalertdialog ();
}
});

Button2.setonclicklistener (New Onclicklistener () {

@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Showlistdialog ();
}
});

Button3.setonclicklistener (New Onclicklistener () {

@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Showsinglechoicedialog ();
}
});

Button4.setonclicklistener (New Onclicklistener () {

@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Showmultichoicedialog ();
}
});


}

/**
* ShowDialog
*/
private void Showalertdialog ()
{
// Alertdialog Alertdialog = new Alertdialog.builder (mainactivity.this). Create ();
// Alertdialog.seticon (R.drawable.ic_launcher);
// Alertdialog.settitle ("system hint");
// Alertdialog.setmessage ("dialog box with Cancel, neutral, OK button");
//// Alertdialog.setbutton (Dialoginterface.button_negative, "Cancel", listener);
Alertdialog.builder Builder = new Alertdialog.builder (mainactivity.this);
Builder.seticon (R.drawable.ic_launcher);
Builder.settitle ("system hint");
Builder.setmessage ("dialog box with Cancel, neutral, OK button");

Are you sure
Builder.setpositivebutton ("OK", new Dialoginterface.onclicklistener () {

@Override
public void OnClick (Dialoginterface dialog, int which) {
TODO auto-generated Method Stub
Showtoast ("Clicked on a certain button");
}
});

Neutral
Builder.setneutralbutton ("neutral", new Dialoginterface.onclicklistener () {

@Override
public void OnClick (Dialoginterface dialog, int which) {
TODO auto-generated Method Stub
Showtoast ("Clicked on neutral button");
}
});

Cancel
Builder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {

@Override
public void OnClick (Dialoginterface dialog, int which) {
TODO auto-generated Method Stub
Showtoast ("Click Cancel Button");
}
});

Builder.show ();
}

/**
* Dialog box with List
*/
private void Showlistdialog ()
{
Final string[] items = new string[] {"Running", "Badminton", "Football"};
Alertdialog.builder Builder = new Alertdialog.builder (mainactivity.this);
Builder.seticon (R.drawable.ic_launcher);
Builder.settitle ("Please select the appropriate sports item");
Builder.setitems (items, new Dialoginterface.onclicklistener () {

@Override
public void OnClick (Dialoginterface dialog, int which) {
TODO auto-generated Method Stub
Showtoast (Items[which]);
}
});
Builder.show ();
}

/**
* A dialog box with a button and a single-selection list
*/
private void Showsinglechoicedialog ()
{
Final string[] items = new string[] {"Standard mode", "Silent Mode", "meeting mode", "Airplane Mode", "Outdoor Mode"};
Builder builder = new Alertdialog.builder (mainactivity.this);
Builder.seticon (R.drawable.ic_launcher);
Builder.settitle ("Please select the scene mode you need");
Builder.setsinglechoiceitems (items, 0, new Dialoginterface.onclicklistener () {

@Override
public void OnClick (Dialoginterface dialog, int which) {
TODO auto-generated Method Stub
Showtoast ("You chose" + Items[which]);
}
});
Builder.setpositivebutton ("OK", null);
Builder.show ();
}

/**
* With multiple-selection list items and a list dialog box that determines the button
*/
private void Showmultichoicedialog ()
{
// Final boolean[] CheckedItems = new boolean[] {false, True, False, True, false};
// string[] items = new string[] {"Plants vs. Zombies", "Angry Birds", "Bubble Dragon", "Happy Farm", "QQ rook", "Super Marie"};

Builder builder = new Alertdialog.builder (mainactivity.this);
Builder.seticon (R.drawable.ic_launcher);
Builder.settitle ("Please choose the game you Like");
Builder.setmultichoiceitems (items, CheckedItems, new Dialoginterface.onmultichoiceclicklistener () {

@Override
public void OnClick (Dialoginterface dialog, int which, Boolean isChecked) {
TODO auto-generated Method Stub
Checkeditems[which] = isChecked;
}
});

Builder.setpositivebutton ("OK", new Dialoginterface.onclicklistener () {

@Override
public void OnClick (Dialoginterface dialog, int which) {
TODO auto-generated Method Stub
String result = "";
for (int i= 0; i < checkeditems.length; i++)
{
if (Checkeditems[i])
{
Result + = Items[i];
Result + = "";
}
}

if (! "". Equals (Result))
{
Showtoast (result);
}
}
});

Builder.show ();
}

/**
*
* @param str
*/
private void Showtoast (String str)
{
Toast.maketext (Mainactivity.this, str, toast.length_long). Show ();
}

@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Inflate the menu; This adds items to the action bar if it is present.
Getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}


@Override
public boolean onoptionsitemselected (MenuItem item) {
Handle Action Bar Item clicks here. The Action Bar would
Automatically handle clicks on the Home/up button, so long
As you specify a the parent activity in Androidmanifest.xml.
int id = item.getitemid ();
if (id = = r.id.action_settings) {
return true;
}
return super.onoptionsitemselected (item);
}
}

Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

"Android" Android in Alertdialog use Daquan

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.