簡單得dialog(入門知識(shí))

前言說明,我寫這個(gè)只是為了自己做筆記記錄一下,如果你學(xué)安卓已經(jīng)可以那么沒必要看了。都是簡單的知識(shí)。我好久沒寫程序了,現(xiàn)在重新寫一次安卓!

首先,定義一個(gè)點(diǎn)擊事件并且對(duì)其進(jìn)行監(jiān)聽,具體的程序如下:

<RelativeLayout

? ? xmlns:android="http://schemas.android.com/apk/res/android"

? ? xmlns:app="http://schemas.android.com/apk/res-auto"

? ? xmlns:tools="http://schemas.android.com/tools"

? ? android:layout_width="match_parent"

? ? android:layout_height="match_parent"

? ? tools:context="com.example.lenovo.ztbndome.MainActivity">

? <TextView

? ? ? android:gravity="center"

? ? ? android:layout_width="wrap_content"

? ? ? android:layout_height="wrap_content"

? ? ? android:id="@+idt_Click"

? ? ? android:textSize="20dp"

? ? ? android:text="@string/Click_txt"

? ? ? />

? ? <TextView

? ? ? ? android:id="@+idt_as"

? ? ? ? android:layout_width="wrap_content"

? ? ? ? android:layout_height="wrap_content"

? ? ? ? android:text="@string/Click_txt"

? ? ? ? android:layout_below="@idt_Click"

? ? ? ? />

</RelativeLayout>

activty部分:


package com.example.lenovo.ztbndome;

import android.content.DialogInterface;

import android.support.v7.app.AlertDialog;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.TextView;

import android.widget.Toast;

public class MainActivity extends AppCompatActivity

{

? ? private TextView txt_click;

? ? @Override

? ? protected void onCreate(Bundle savedInstanceState)

? ? {

? ? ? ? super.onCreate(savedInstanceState);

? ? ? ? setContentView(R.layout.activity_main);

? ? ? ? ////通過id尋找到txt_click這個(gè)控件

? ? ? ? txt_click= (TextView) findViewById(R.id.txt_Click);

? ? ? ? ///為txt_click設(shè)置一個(gè)監(jiān)聽事件

? ? ? ? txt_click.setOnClickListener(new View.OnClickListener()

? ? ? ? {

? ? ? ? ? ? @Override

? ? ? ? ? ? public void onClick(View v)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? ///利用工廠模式制作一個(gè)Dialog

? ? ? ? ? ? ? ? AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);

? ? ? ? ? ? ? ? builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

? ? ? ? ? ? ? ? ? ? @Override

? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int which)

? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? Toast.makeText(getBaseContext(),"使用正確",Toast.LENGTH_SHORT).show();

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? }).setNegativeButton("No", new DialogInterface.OnClickListener() {

? ? ? ? ? ? ? ? ? ? @Override

? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int which)

? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? dialog.cancel();

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? }).show();

? ? ? ? ? ? ? ? AlertDialog alert=builder.create();

? ? ? ? ? ? }

? ? ? ? });

? ? }

}


以上是xml部分和activity的,也是全部了,這里的想說一下如果你看過工廠設(shè)計(jì)模式的話,那么這里的dialog本質(zhì)上就是利用了工廠設(shè)計(jì)模式做出來的,有興趣的可以去看看源碼!!



圖片發(fā)自簡書App

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容