zoukankan      html  css  js  c++  java
  • android 简单对话框实现

    1.实现效果:

    原始界面:                                       点击按钮之后,显示对话框                点击对话框后的确定按钮后,显示一段文字

               

    布局文件:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.dialoginterface.MainActivity">
    <TextView
    android:layout_marginTop="30dp"
    android:layout_marginLeft="50dp"
    android:textSize="30sp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello" />
    <Button
    android:layout_marginLeft="50dp"
    android:id="@+id/button"
    android:text="@string/str_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
    <TextView
    android:textSize="30sp"
    android:layout_marginLeft="50dp"
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
    </LinearLayout>

    java文件:
    public class MainActivity extends Activity {
    private Button bt;
    private TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    bt=(Button)findViewById(R.id.button);
    tv=(TextView)findViewById(R.id.text);
    bt.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    //创建一个对话框对象
    AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
    //对对话框内容进行定义
    builder.setTitle(R.string.app_about);
    builder.setMessage(R.string.app_about_msg);
    //定义对话框内容的点击事件,注意后面还有个show,否则不会显示对话框
    builder.setPositiveButton(R.string.str_ok, new DialogInterface.OnClickListener(){
    @Override
    //定义点击对话框按钮后执行的动作,这里是添加一个textview的内容
    public void onClick(DialogInterface dialog, int which) {
    tv.setText("这是点击对话框按钮后,才会显示的内容!");
    }
    }).show();
    }
    });
    }
    }
  • 相关阅读:
    C++笔记(2018/2/6)
    2017级面向对象程序设计寒假作业1
    谁是你的潜在朋友
    A1095 Cars on Campus (30)(30 分)
    A1083 List Grades (25)(25 分)
    A1075 PAT Judge (25)(25 分)
    A1012 The Best Rank (25)(25 分)
    1009 说反话 (20)(20 分)
    A1055 The World's Richest(25 分)
    A1025 PAT Ranking (25)(25 分)
  • 原文地址:https://www.cnblogs.com/xy95/p/5860832.html
Copyright © 2011-2022 走看看