純程式碼實現 Android 登陸布局

來源:互聯網
上載者:User
        下面代碼實現的是 登陸 ,一點XML 配置都沒有.看起來顯然沒有之前的使用XML配置 例子清晰.
/**
 * 
 */
package com.birds.android.login;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
/**
 * @author birds
 * @date 2008-11-22
 * @file $
 */
public class LoginCodeUI extends Activity implements OnClickListener {
    private static final int BUTTON_ID_OK = 5000000;
    private static final int BUTTON_ID_CANCEL = 6000000;
    private static final int TEXT_ID_ONE = 111111;
    private static final int TEXT_PASSWD = 222222;
    /*
     * (non-Javadoc)
     * 
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TableLayout tableLayout = new TableLayout(this);
        setContentView(tableLayout);
        TableRow row1 = new TableRow(this);
        tableLayout.addView(row1);
        TextView view1 = new TextView(this);
        view1.setText("帳號名稱:");
        view1.setWidth(100);
        EditText text1 = new EditText(this);
        text1.setWidth(150);
        text1.setSingleLine();
        text1.setFilters(new InputFilter[] { new InputFilter.LengthFilter(15) });
        text1.setId(TEXT_ID_ONE);
        row1.addView(view1);
        row1.addView(text1);
        TableRow row2 = new TableRow(this);
        tableLayout.addView(row2);
        TextView view2 = new TextView(this);
        view2.setWidth(100);
        view2.setText("帳號密碼:");
        EditText text2 = new EditText(this);
        text2.setSingleLine();
        text2.setWidth(150);
        // 設定為Cipher 模式
        text2.setTransformationMethod(new PasswordTransformationMethod());
        // 設定最大長度,為15
        text2.setFilters(new InputFilter[] { new InputFilter.LengthFilter(15) });
        text2.setId(TEXT_PASSWD);
        row2.addView(view2);
        row2.addView(text2);
        TableRow row3 = new TableRow(this);
        tableLayout.addView(row3);
        Button b1 = new Button(this);
        b1.setText("確定");
        b1.setId(BUTTON_ID_OK);
        b1.setOnClickListener(this);
        Button b2 = new Button(this);
        b2.setText("取消");
        b2.setId(BUTTON_ID_CANCEL);
        b2.setOnClickListener(this);
        row3.addView(b1);
        row3.addView(b2);
    }
    @Override
    public void onClick(View v) {
        EditText t1 = (EditText) findViewById(TEXT_ID_ONE);
        EditText t2 = (EditText) findViewById(TEXT_PASSWD);
        if (v.getId() == BUTTON_ID_OK) {
            Builder alertDialog = new AlertDialog.Builder(this);
            alertDialog.setPositiveButton("OK",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface v, int btn) {
                            v.cancel();
                        }
                    });
            alertDialog.setTitle("輸入的資訊:" + t1.getText() + " 密碼:"
                    + t2.getText());
            alertDialog.show();
        } else if (v.getId() == BUTTON_ID_CANCEL) {
            t1.setText("");
            t2.setText("");
        }
    }
}
   xml配置布局的靈活性明顯,代碼主要 是邏輯功能的實現, 有點 MVC 的思想 .
   xml為視圖配置,邏輯代碼另寫class,android事件監聽 為控制器.
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.