alertDialog与PositiveButton和NegativeButton从XML [英] alertDialog with PositiveButton and NegativeButton from xml
本文介绍了alertDialog与PositiveButton和NegativeButton从XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经警告对话框无一不PositiveButton和NegativeButton选址编程,我想从XML布局检索它们,我尝试做,但即时通讯新Android开发,
任何帮助将AP preciated,谢谢。
MainActivity:
公共类MainActivity延伸活动{
最终的上下文语境=这一点;
私人Button按钮;
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
TextView的电视=(TextView中)findViewById(R.id.introclusion_tv1);
tv.setTypeface(FontFactory.getBFantezy(getBaseContext()));
TextView的TV1 =(TextView中)findViewById(R.id.introclusion_tv2);
tv1.setTypeface(FontFactory.getBFantezy(getBaseContext()));
tv1.setText(Html.fromHtml(的getString(R.string.introclusion)));
按钮=(按钮)findViewById(R.id.button1);
//添加按钮监听器
button.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(查看为arg0){
LayoutInflater李= LayoutInflater.from(arg0.getContext());
最后查看dialog_layoutView = li.inflate(R.layout.dialog_layout,NULL);
AlertDialog.Builder alertDialogBuilder =新AlertDialog.Builder(arg0.getContext());
alertDialogBuilder.setView(dialog_layoutView);
alertDialogBuilder
.setCancelable(假)
.setPositiveButton(OK,
新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话框,INT ID){
EditText上的用户名=(EditText上)dialog_layoutView.findViewById(R.id.txt_name);
的EditText密码=(EditText上)dialog_layoutView.findViewById(R.id.password);
如果(username.getText()的toString()长度()> 0安培;&安培; password.getText()的toString()长度()> 0){
如果(username.getText()的toString()等于(测试)及。&安培; password.getText()的toString()等于(测试)。){
意向意图=新的意图(MainActivity.this,Text.class);
startActivity(意向);
完();}
其他{
//得到你的custom_toast.xml布局
LayoutInflater充气= getLayoutInflater();
查看布局= inflater.inflate(R.layout.custom_toast,
(ViewGroup中)findViewById(R.id.custom_toast));
//设置一个虚拟形象
ImageView的形象=(ImageView的)layout.findViewById(R.id.image_toast);
image.setImageResource(R.drawable.ic_launcher);
//设置消息
TextView的文字=(TextView的)layout.findViewById(R.id.text_toast);
text.setText(错误的用户名或密码);
//吐司...
吐司面包=新吐司(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL,0,0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(布局);
toast.show();}}}
})
.setNegativeButton(取消,
新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话框,
INT编号){
dialog.cancel();
}
});
//创建警报对话框
AlertDialog alertDialog = alertDialogBuilder.create();
// 展示下
alertDialog.show();
}
});
}
}
dialog_layout.xml:
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=垂直
机器人:填充=10SP>
<的EditText
机器人:ID =@ + ID / txt_name
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:提示=@字符串/ dialog_uname
机器人:单线=真正的>
<不是requestFocus />
< /的EditText>
<的EditText
机器人:ID =@ + ID /密码
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:EMS =10
机器人:inputType =textPassword>
< /的EditText>
< RelativeLayout的
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT>
<按钮
机器人:ID =@ + ID / btn_ok
机器人:layout_width =120dp
机器人:layout_height =WRAP_CONTENT
机器人:文本=@字符串/ dialog_submit/>
<按钮
机器人:ID =@ + ID / btn_cancel
机器人:layout_width =120dp
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentTop =真
机器人:layout_marginLeft =10dp
机器人:layout_toRightOf =@ + ID / btn_ok
机器人:文本=@字符串/ dialog_cancel/>
< / RelativeLayout的>
编辑:
公共类MainActivity延伸活动{
最终的上下文语境=这一点;
私人Button按钮;
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
TextView的电视=(TextView中)findViewById(R.id.introclusion_tv1);
tv.setTypeface(FontFactory.getBFantezy(getBaseContext()));
TextView的TV1 =(TextView中)findViewById(R.id.introclusion_tv2);
tv1.setTypeface(FontFactory.getBFantezy(getBaseContext()));
tv1.setText(Html.fromHtml(的getString(R.string.introclusion)));
按钮=(按钮)findViewById(R.id.button1);
//添加按钮监听器
button.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(查看为arg0){
LayoutInflater李= LayoutInflater.from(arg0.getContext());
最后查看dialog_layoutView = li.inflate(R.layout.dialog_layout,NULL);
AlertDialog.Builder alertDialogBuilder =新AlertDialog.Builder(arg0.getContext());
alertDialogBuilder.setView(dialog_layoutView);
alertDialogBuilder
.setCancelable(假);
/////////
按钮okBtn =(按钮)dialog_layoutView.findViewById(R.id.btn_ok);
okBtn.setOnClickListener(新OnClickListener()
{
@覆盖
公共无效的onClick(视图v)
{
EditText上的用户名=(EditText上)dialog_layoutView.findViewById(R.id.txt_name);
的EditText密码=(EditText上)dialog_layoutView.findViewById(R.id.password);
如果(username.getText()的toString()长度()> 0安培;&安培; password.getText()的toString()长度()> 0){
如果(username.getText()的toString()等于(测试)及。&安培; password.getText()的toString()等于(测试)。){
意向意图=新的意图(MainActivity.this,Text.class);
startActivity(意向);
完();}
其他{
//得到你的custom_toast.xml布局
LayoutInflater充气= getLayoutInflater();
查看布局= inflater.inflate(R.layout.custom_toast,
(ViewGroup中)findViewById(R.id.custom_toast));
//设置一个虚拟形象
ImageView的形象=(ImageView的)layout.findViewById(R.id.image_toast);
image.setImageResource(R.drawable.ic_launcher);
//设置消息
TextView的文字=(TextView的)layout.findViewById(R.id.text_toast);
text.setText(错误的用户名或密码);
//吐司...
吐司面包=新吐司(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL,0,0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(布局);
toast.show();}}}}
);
按钮cancelBtn =(按钮)dialog_layoutView.findViewById(R.id.btn_cancel);
cancelBtn.setOnClickListener(新OnClickListener()
{
@覆盖
公共无效的onClick(视图v)
{
dialog.cancel();
}
});
///////
//创建警报对话框
AlertDialog alertDialog = alertDialogBuilder.create();
// 展示下
alertDialog.show();
}
});
}
}
解决方案
您可以使用 dialog_layout
来检索您的按钮
为你做你的的EditText
按钮okBtn =(按钮)dialog_layout.findViewById(R.id.btn_ok);
按钮cancelBtn =(按钮)dialog_layout.findViewById(R.id.btn_cancel);
然后设置 onClickListener
取值
okBtn.setOnClickListener(新OnClickListener()
{
@覆盖
公共无效的onClick(视图v)
{
//一些code
}
});
cancelBtn.setOnClickListener(新OnClickListener()
{
@覆盖
公共无效的onClick(视图v)
{
//一些code
}
});
I have alert dialog and both PositiveButton and NegativeButton sited programmatically , i want to retrieve them from xml layout ,i try to do it but im new to android development ,
any help will be appreciated , thanks.
MainActivity:
public class MainActivity extends Activity {
final Context context = this;
private Button button;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv=(TextView)findViewById(R.id.introclusion_tv1);
tv.setTypeface(FontFactory.getBFantezy(getBaseContext()));
TextView tv1=(TextView)findViewById(R.id.introclusion_tv2);
tv1.setTypeface(FontFactory.getBFantezy(getBaseContext()));
tv1.setText(Html.fromHtml(getString(R.string.introclusion)));
button = (Button) findViewById(R.id.button1);
// add button listener
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
LayoutInflater li = LayoutInflater.from(arg0.getContext());
final View dialog_layoutView = li.inflate(R.layout.dialog_layout, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(arg0.getContext());
alertDialogBuilder.setView(dialog_layoutView);
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
EditText username = (EditText) dialog_layoutView.findViewById(R.id.txt_name);
EditText password = (EditText) dialog_layoutView.findViewById(R.id.password);
if(username.getText().toString().length() > 0 && password.getText().toString().length() > 0 ) {
if(username.getText().toString().equals("test") && password.getText().toString().equals("test")) {
Intent intent = new Intent(MainActivity.this,Text.class);
startActivity(intent);
finish();}
else{
// get your custom_toast.xml layout
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,
(ViewGroup) findViewById(R.id.custom_toast));
// set a dummy image
ImageView image = (ImageView) layout.findViewById(R.id.image_toast);
image.setImageResource(R.drawable.ic_launcher);
// set a message
TextView text = (TextView) layout.findViewById(R.id.text_toast);
text.setText("Wrong username or password");
// Toast...
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();}}}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
}
}
dialog_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10sp" >
<EditText
android:id="@+id/txt_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/dialog_uname"
android:singleLine="true" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword" >
</EditText>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btn_ok"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="@string/dialog_submit" />
<Button
android:id="@+id/btn_cancel"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/btn_ok"
android:text="@string/dialog_cancel" />
</RelativeLayout>
EDIT :
public class MainActivity extends Activity {
final Context context = this;
private Button button;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv=(TextView)findViewById(R.id.introclusion_tv1);
tv.setTypeface(FontFactory.getBFantezy(getBaseContext()));
TextView tv1=(TextView)findViewById(R.id.introclusion_tv2);
tv1.setTypeface(FontFactory.getBFantezy(getBaseContext()));
tv1.setText(Html.fromHtml(getString(R.string.introclusion)));
button = (Button) findViewById(R.id.button1);
// add button listener
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
LayoutInflater li = LayoutInflater.from(arg0.getContext());
final View dialog_layoutView = li.inflate(R.layout.dialog_layout, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(arg0.getContext());
alertDialogBuilder.setView(dialog_layoutView);
alertDialogBuilder
.setCancelable(false);
/////////
Button okBtn= (Button) dialog_layoutView.findViewById(R.id.btn_ok);
okBtn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
EditText username = (EditText) dialog_layoutView.findViewById(R.id.txt_name);
EditText password = (EditText) dialog_layoutView.findViewById(R.id.password);
if(username.getText().toString().length() > 0 && password.getText().toString().length() > 0 ) {
if(username.getText().toString().equals("test") && password.getText().toString().equals("test")) {
Intent intent = new Intent(MainActivity.this,Text.class);
startActivity(intent);
finish();}
else{
// get your custom_toast.xml layout
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,
(ViewGroup) findViewById(R.id.custom_toast));
// set a dummy image
ImageView image = (ImageView) layout.findViewById(R.id.image_toast);
image.setImageResource(R.drawable.ic_launcher);
// set a message
TextView text = (TextView) layout.findViewById(R.id.text_toast);
text.setText("Wrong username or password");
// Toast...
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();}}}}
);
Button cancelBtn = (Button) dialog_layoutView.findViewById(R.id.btn_cancel);
cancelBtn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
dialog.cancel();
}
});
///////
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
}
}
解决方案
You can use your dialog_layout
to retrieve your Buttons
as you did with your EditText
Button okBtn= (Button) dialog_layout.findViewById(R.id.btn_ok);
Button cancelBtn = (Button) dialog_layout.findViewById(R.id.btn_cancel);
then set you onClickListener
s
okBtn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// some code
}
});
cancelBtn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// some code
}
});
这篇关于alertDialog与PositiveButton和NegativeButton从XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文