<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.iii.lcpan.alertdialog.MainActivity">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="@string/cel_lbl" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="numberDecimal"
            android:ems="10"
            android:id="@+id/celsius" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/btn_lbl"
            android:id="@+id/submit" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="@string/fah_lbl"
            android:id="@+id/fahrenheit" />

    </LinearLayout>
</RelativeLayout>



<resources>
    <string name="app_name">AlertDialog</string>
    <string name="cel_lbl">攝氏:</string>
    <string name="fah_lbl">華氏:</string>
    <string name="aaa">測試</string>
    <string name="btn_lbl">轉換成華式</string>
    <string name="title">錯誤</string>
    <string name="msg">必須輸入值</string>
    <string name="confirm">確認</string>
</resources>



package com.iii.lcpan.alertdialog;

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.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity  implements View.OnClickListener{
    private EditText cel;
    private TextView fah;
    private Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        cel=(EditText) findViewById(R.id.celsius);
        fah=(TextView) findViewById(R.id.fahrenheit);
        btn=(Button) findViewById(R.id.submit);
        btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        try {
            double f = (Double.parseDouble(cel.getText().toString())) * 9 / 5 + 32;
            fah.setText(getText(R.string.fah_lbl) + (Double.valueOf(f).toString()));
        }catch(Exception e){
            showAlertDialog();
//            Toast.makeText(this,R.string.msg,Toast.LENGTH_LONG).show();
        }
    }
    private void showAlertDialog(){
       AlertDialog.Builder builder=new AlertDialog.Builder(this);
        //this是MainActivity物件,此物件是Context的兒子
        //new AlertDialog.Builder(this); 代表這個Alert要跟MainActivity這個activity物件作連接
        //        public Builder(Context context) {
        //            this(context, resolveDialogTheme(context, 0));
        //        }
        builder.setTitle(R.string.title); //設定彈跳出的視窗的標題
        builder.setMessage(R.string.msg); //設定彈跳視窗內文
        //setPositiveButton(string , 按完按鈕之後要做的事情)
        builder.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        fah.setText(getText(R.string.aaa));
                        //這個setText只能用設定在string.xml中文字,不能直接打字
                    }
                }
        );
        builder.show();
        //-------------------------------------------------------------
         //另一種寫法(串起來)
//        new AlertDialog.Builder(this)
//        .setTitle(R.string.title)
//        .setMessage(R.string.msg)
//        .setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() { 
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//
//                    }
//                }
//        )
//        .show();
    }
}
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 乙方 的頭像
    乙方

    學習筆記專區

    乙方 發表在 痞客邦 留言(0) 人氣()