Dialog

Z Android Wiki
Přejít na: navigace, hledání

Jednoduché zobrazení dialogu[editovat]

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.Toast;

public class NovyDialog extends Activity {

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		final FrameLayout fl = new FrameLayout(this);
		final EditText input = new EditText(this);
		input.setGravity(Gravity.CENTER);

		fl.addView(input, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT,
				FrameLayout.LayoutParams.WRAP_CONTENT));//novy laiout,ve kterem se odevre dialog

		input.setText("pokus");
		new AlertDialog.Builder(this).setView(fl).setTitle("Napis text").setPositiveButton("OK",//tlacitko OK
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface d, int which) {
						d.dismiss();//zruseni dialogu
						 Toast.makeText(getBaseContext(), "Value: " + input.getText().toString(), Toast.LENGTH_LONG).show();
					//trida Toast pro jednoduche vypsani textu (pro kontrolu)
					}
				}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {//tlacitko Cancel
			@Override
			public void onClick(DialogInterface d, int which) {
				d.dismiss();//zruseni dialogu
			}
		}).create().show();//ukaz dialog
	}
}