這個範例只是簡單的把帳號資料傳到下一個畫面
中間並沒有經過資料庫跟資料驗證
有興趣可以做個資料驗證,就可以做個簡單的登入程式

在專案裡的res/layout資料夾新增兩個xml檔如下:

login.xml:

<?xml version="1.0" encoding="utf-8"?>

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

    android:layout_width="match_parent"

    android:layout_height="match_parent" >



    <TextView

        android:id="@+id/textView01"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentLeft="true"

        android:layout_alignParentTop="true"

        android:layout_marginTop="18dp"

        android:text="帳號" />



    <TextView

        android:id="@+id/textView02"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentLeft="true"

        android:layout_below="@+id/textView01"

        android:layout_marginTop="14dp"

        android:text="密碼" />



    <EditText

        android:id="@+id/editText01"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignTop="@+id/textView01"

        android:layout_toRightOf="@+id/textView01"

        android:ems="10"

        android:inputType="textPersonName" >



        <requestFocus />

    </EditText>



    <EditText

        android:id="@+id/editText02"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignTop="@+id/textView02"

        android:layout_toRightOf="@+id/textView02"

        android:ems="10"

        android:inputType="textPassword" />



    <Button

        android:id="@+id/button01"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignRight="@+id/editText02"

        android:layout_below="@+id/editText02"

        android:layout_marginTop="16dp"

        android:text="送出" />



</RelativeLayout>





result.xml:


<?xml version="1.0" encoding="utf-8"?>


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


    android:layout_width="match_parent"


    android:layout_height="match_parent" >




    <TextView


        android:id="@+id/textView00"


        android:layout_width="wrap_content"


        android:layout_height="wrap_content"


        android:layout_alignParentLeft="true"


        android:layout_alignParentTop="true"


        android:text="Large Text"


        android:textAppearance="?android:attr/textAppearanceLarge" />




</RelativeLayout>


然後編寫MainActivity.java檔如下


public class MainActivity extends Activity {



TextView textView00 = null;


EditText editText01 = null;


EditText editText02 = null;


Button button01 =null;




public void onCreate(Bundle savedInstanceState){


setContentView(R.layout.login);




editText01 = (EditText) findViewById(R.id.editText01);


button01 = (Button) findViewById(R.id.button01);


button01.setOnClickListener(new View.OnClickListener() {


@Override


public void onClick(View v) {


String name = editText01.getText().toString();


setContentView(R.layout.result);


textView00 = (TextView) findViewById(R.id.textView00);


textView00.setText("你好:"+name);


}


});




}



}

arrow
arrow
    全站熱搜

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