首先自定义AlertDialog需要先写一个自定义的布局来描述我们自定义的Alertdialog
新建runstop_check.xml代码如下
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="290dp" android:layout_height="380dp"> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="50dp"> <TextView android:textSize="25dp" android:text="运动时间:" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/runstop_time" android:textSize="25dp" android:text="00.00.00" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="50dp"> <TextView android:textSize="25dp" android:text="运动距离:" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/runstop_mile" android:textSize="25dp" android:text="00.00km" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="50dp"> <TextView android:textSize="25dp" android:text="平均速度:" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:textSize="25dp" android:id="@+id/runstop_speed" android:text="00.00km/h" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="50dp"> <TextView android:textSize="25dp" android:text="步数:" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/runstop_footnum" android:textSize="25dp" android:text="6000" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="50dp"> <TextView android:textSize="25dp" android:text="步频/步距:" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:textSize="25dp" android:id="@+id/runstop_bupin_buju" android:text="70次/0.7米" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:background="#000000" android:layout_width="match_parent" android:layout_height="1dp"/> <LinearLayout android:layout_width="match_parent" android:layout_height="10dp"></LinearLayout> <TextView android:textSize="20dp" android:text="此次运动将会结束,确认结束?" android:layout_width="match_parent" android:layout_height="wrap_content" /> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="50dp"> <Button android:id="@+id/run_goon" android:background="@android:color/transparent" android:textSize="30dp" android:text="再坚持一下" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/run_stop" android:background="@android:color/transparent" android:textSize="20dp" android:text="结束" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout>
然后再MainActivity中加入如下代码
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); runspeed=(TextView)findViewById(R.id.runspeed); mile=(TextView)findViewById(R.id.mile) ; stoprun=(ImageButton)findViewById(R.id.runstop); stoprun.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { final View alertDialogView = getLayoutInflater ().inflate (R.layout.runstop_check, null, false); AlertDialog.Builder loginAlertDialog = new AlertDialog.Builder (MainActivity.this); loginAlertDialog.setView (alertDialogView); final AlertDialog dialog = loginAlertDialog.show ();// loginAlertDialog.setPositiveButton ("OK", new DialogInterface.OnClickListener () { // @Override // public void onClick(DialogInterface dialogInterface, int i) { // // // } // }); } });
再MainActivity对应的xml中只有一个button按钮名字是stoprun我就不沾代码了
下面记录一下如何通过按钮点击事件来关闭AlertDialog(一般点击AlerDialog外部即可关闭,但是有时会有按钮关闭的形式,我们记录下按钮关闭的方法)
在上图注释代码上端加上如下的按钮点击事件代码,AlertDialog.Builder是没有dismiss方法的所以我们 final AlertDialog dialog = loginAlertDialog.show ();通过AlertDialog.Builder生成一个Alert Dialog然后在通过dismiss方法关闭AlertDialog
goon.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } });