标签:android style blog http ar color os sp java
驾驭命运的舵是奋斗。不抱有一丝幻想,不放弃一点机会,不停止一日努力。
本讲内容:Dialog 对话框
例六:信息内容是一组简单列表项
下面是MainActivity.java主界面文件:
public class MainActivity extends Activity{ private Button b; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b=(Button) findViewById(R.id.button); b.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { Dialog(); } }); } private void Dialog() { new AlertDialog.Builder(this).setTitle("列表框").setItems( new String[] { "Item1", "Item2" }, null).setNegativeButton( "确定", null).show(); } }
例七:信息内容是一个自定义的布局
下面是新建的res/layout/dialog.xml
布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/dialog"> <TextView android:id="@+id/tv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="姓名:"/> <EditText android:id="@+id/edit" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
public class MainActivity extends Activity{ private Button b; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b=(Button) findViewById(R.id.button); b.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { Dialog(); } }); } private void Dialog() { LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.dialog,(ViewGroup) findViewById(R.id.dialog)); new AlertDialog.Builder(this).setTitle("自定义布局").setView(layout) .setPositiveButton("确定", null) .setNegativeButton("取消", null).show(); } }
本讲就到这里,Take your time and enjoy it
标签:android style blog http ar color os sp java
原文地址:http://blog.csdn.net/liguojin1230/article/details/41344649