20170114 輸入電話專用的EditText (20161229完成)
Android 練習的成果,輸入電話專用的EditText!
簡單的EditText輸入和輸出
以下是程式碼
//練習寫紀錄電話簿EditText
//20161229劉彥廷
MainActivity.java
package com.example.user.ch0003;
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;
public class MainActivity0003 extends AppCompatActivity {
public EditText sname,fname,pphone;
public TextView txv;
public Button button01;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main0003);
sname = (EditText) this.findViewById(R.id.editText);
fname = (EditText) this.findViewById(R.id.editText2);
pphone = (EditText) this.findViewById(R.id.editText6);
txv = (TextView) this.findViewById(R.id.txv);
button01 = (Button) this.findViewById(R.id.button);
}
public void onclick (View v){
txv.setText(sname.getText().toString()+
fname.getText()+
"的電話是"+pphone.getText());
}
activvity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main0003"
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.example.user.ch0003.MainActivity0003"
android:weightSum="1"
android:orientation="vertical"
android:background="#e0e0d1">
<LinearLayout
android:orientation="horizontal"
android:layout_width="279dp"
android:layout_height="wrap_content">
<TextView
android:text="@string/TextView1"
android:layout_width="68dp"
android:id="@+id/textView"
android:layout_height="wrap_content"
android:textColor="#000000" />
<EditText
android:layout_width="match_parent"
android:layout_height="68dp"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/editText"
android:hint="@string/name" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1">
<TextView
android:text="@string/TextView2"
android:layout_height="wrap_content"
android:id="@+id/textView2"
android:layout_width="68dp"
android:textColor="#000000" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/editText2"
android:hint="@string/name2" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1">
<TextView
android:text="@string/TextView3"
android:layout_width="68dp"
android:layout_height="35dp"
android:id="@+id/textView3"
android:textColor="#000000" />
<EditText
android:layout_width="wrap_content"
android:layout_height="57dp"
android:ems="10"
android:id="@+id/editText6"
android:inputType="phone"
android:hint="@string/pphone" />
</LinearLayout>
<Button
android:layout_width="282dp"
android:layout_height="wrap_content"
android:id="@+id/button"
android:text="@string/onclick"
android:onClick="onclick" />
<TextView
android:layout_width="279dp"
android:layout_height="77dp"
android:id="@+id/txv"
android:textSize="24sp"
android:background="#e0e0d1"
android:textColor="#0080ff" />
</LinearLayout>
strings.xml
<resources>
<string name="app_name">輸入電話專用的EditText
</string>
<string name="TextView1">姓:</string>
<string name="name">請輸入姓氏</string>
<string name="TextView2">名:</string>
<string name="name2">請輸入名稱</string>
<string name="TextView3">電話:</string>
<string name="pphone">請輸入號碼</string>
<string name="onclick" translatable="false">確定</string>
</resources>
//20161229劉彥廷
MainActivity.java
package com.example.user.ch0003;
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;
public class MainActivity0003 extends AppCompatActivity {
public EditText sname,fname,pphone;
public TextView txv;
public Button button01;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main0003);
sname = (EditText) this.findViewById(R.id.editText);
fname = (EditText) this.findViewById(R.id.editText2);
pphone = (EditText) this.findViewById(R.id.editText6);
txv = (TextView) this.findViewById(R.id.txv);
button01 = (Button) this.findViewById(R.id.button);
}
public void onclick (View v){
txv.setText(sname.getText().toString()+
fname.getText()+
"的電話是"+pphone.getText());
}
activvity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main0003"
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.example.user.ch0003.MainActivity0003"
android:weightSum="1"
android:orientation="vertical"
android:background="#e0e0d1">
<LinearLayout
android:orientation="horizontal"
android:layout_width="279dp"
android:layout_height="wrap_content">
<TextView
android:text="@string/TextView1"
android:layout_width="68dp"
android:id="@+id/textView"
android:layout_height="wrap_content"
android:textColor="#000000" />
<EditText
android:layout_width="match_parent"
android:layout_height="68dp"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/editText"
android:hint="@string/name" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1">
<TextView
android:text="@string/TextView2"
android:layout_height="wrap_content"
android:id="@+id/textView2"
android:layout_width="68dp"
android:textColor="#000000" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/editText2"
android:hint="@string/name2" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1">
<TextView
android:text="@string/TextView3"
android:layout_width="68dp"
android:layout_height="35dp"
android:id="@+id/textView3"
android:textColor="#000000" />
<EditText
android:layout_width="wrap_content"
android:layout_height="57dp"
android:ems="10"
android:id="@+id/editText6"
android:inputType="phone"
android:hint="@string/pphone" />
</LinearLayout>
<Button
android:layout_width="282dp"
android:layout_height="wrap_content"
android:id="@+id/button"
android:text="@string/onclick"
android:onClick="onclick" />
<TextView
android:layout_width="279dp"
android:layout_height="77dp"
android:id="@+id/txv"
android:textSize="24sp"
android:background="#e0e0d1"
android:textColor="#0080ff" />
</LinearLayout>
strings.xml
<resources>
<string name="app_name">輸入電話專用的EditText
</string>
<string name="TextView1">姓:</string>
<string name="name">請輸入姓氏</string>
<string name="TextView2">名:</string>
<string name="name2">請輸入名稱</string>
<string name="TextView3">電話:</string>
<string name="pphone">請輸入號碼</string>
<string name="onclick" translatable="false">確定</string>
</resources>
MralueOcon-ku Richard Gonzalez https://wakelet.com/wake/n2SEUMCo_g06zbcS6ngki
回覆刪除proserimber