前言說明,我寫這個(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ì)模式做出來的,有興趣的可以去看看源碼!!
