安卓關于EditText的簡單用法及intent傳輸ArrayList
-
- EditText的簡單用法
- 執行個體
- 活動
- intent進行ArrayList傳遞
EditText的簡單用法
EditText,文本編輯框,在現實生活中使用很多很多,比如登入頁面的賬号密碼輸入框,打電話時輸入電話号碼,發郵件寫的郵箱,而Android Studio又仔細對這些進行細分(很實用),比如一般的文字編輯框,使用Plain Text,一些可見數字用Number,一些不可見數字字元集合用Password,需求不同,顯示方式也不同,放張圖,就不一一說了。

執行個體
這裡來一個執行個體,寫一個學生的姓名,年齡,性别并顯示出來,使用LinearLayout布局,三個編輯框都一樣,直接複制粘貼改一下就行了,為了節省空間我這裡就放一個,xml代碼如下。
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:hint="@string/name"
android:text="" />
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" />
values下的strings添加:
<string name="name">姓名</string>
ems屬性表示的是編輯框的長度占幾個字元。
inputType表示編輯框類型。
hint屬性是在使用者還沒有輸入的時候進行的半透明提示
這是XML的效果圖:
活動
接下來寫活動,要達到使用者寫完送出以後顯示出來。
先将EditText、Button、textView作為私有的全局變量,友善後面的使用,用findViewById()通過id号找到對應的控件,都是一樣的方法,寫完一個直接copy成下一個
public class MainActivity extends Activity {
private Button button1;
private Button button2;
private Button button3;
EditText editText1;
EditText editText2;
EditText editText3;
TextView textView1;
TextView textView2;
TextView textView3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=findViewById(R.id.button1);
button2=findViewById(R.id.button2);
editText1=findViewById(R.id.editText1);
editText2=findViewById(R.id.editText2);
editText3=findViewById(R.id.editText3);
textView1=findViewById(R.id.textView1);
textView2=findViewById(R.id.textView2);
textView3=findViewById(R.id.textView3);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//參數為(名字,模式),MODE_PRIVATE為預設模式,代表該檔案是私有資料,隻能被應用本身
通路,在該模式下寫入的内容會覆寫原檔案的内容。
//除了這種模式還有MODE_WORLD_REDABLE(可以被其他應用讀取)
//安卓為了應用共享安全,這樣的方法已經行不通了
SharedPreferences.Editor
editor=getSharedPreferences("cong",MODE_PRIVATE).edit();
editor.putString("name",editText1.getText().toString());
//這裡要進行類型轉換
editor.putInt("age",Integer.parseInt(editText2.getText().toString()));
editor.putString("sex",editText3.getText().toString());
editor.apply();//無傳回值
editor.commit();//直接送出一個傳回值,這是同步存儲的方式
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences pref=getSharedPreferences("cong",MODE_PRIVATE);
//根據key擷取值,後面為預設值
String name=pref.getString("name","");
int age=pref.getInt("age",0);
String str=age+"";
String sex=pref.getString("sex","");
//寫入到textview中
textView1.setText(name);
textView2.setText(str);
textView3.setText(sex);
}
});
}}
效果圖:
intent進行ArrayList傳遞
以上就是Editor的簡單用法了
下面講講,如何用intent進行ArrayList傳遞,當我們的編輯框要反複使用并進行資料的記錄時前面的方法就不好用了。
1、在XML中新添加一個按鈕
2、建立一個空白的活動,并在XML中添加一個ListView,同樣是LinearLayout布局
```css
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
3、建立一個學生類并添加Serializable接口。(為什麼要序列化後面會用到)
學生的屬性有name(名字),age(年齡),sex(性别)。,對着三個進行get、set、構造方法、toString方法生成。
public class Student implements Serializable {
String name; int age; String sex;
@Override public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", sex='" + sex + '\'' + '}'; }
public Student() { }
public Student(String name, int age, String sex) {
this.name = name;
this.age = age;
this.sex = sex; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
public String getSex() { return sex; }
public void setSex(String sex) { this.sex = sex; }}
4、修改剛才的代碼,将不用的注釋掉。
全局添加:
//按鈕3
private Button button3;
//student的ArrayList集合
ArrayList<Student> students=new ArrayList();
//建立一個學生對象
Student student=new Student();
//onCreate添加:
button3=findViewById(R.id.button3);
//按鈕1(送出)的監聽事件改為:
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//對student進行三個屬性的指派
student=new Student(editText1.getText().toString(),Integer.parseInt(editText2.getText().toString()),editText3.getText().toString());
// 送出
students.add(student);
//送出完以後進行清空
editText1.setText("");
editText2.setText("");
editText3.setText("");
}
});
//按鈕2(顯示)的全部注釋
//添加按鈕三的監聽事件:
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//從主活動傳到剛才建立的界面
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
Bundle bundle=new Bundle();
//因為傳的是一個ArrayList數組,是以使用bundle并進行序列化
bundle.putSerializable("students",students);
intent.putExtras(bundle);
startActivity(intent);
}
});
//然後修改Main2Activity的界面:
ublic class Main2Activity extends AppCompatActivity {
String[] str={"","","","","","","","","",""};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
//這裡可能會發送資料傳輸異常是以要進行try catch
try {
//擷取Intent
Bundle bundle=getIntent().getExtras();
//用一個ArrayList<泛型>接收它,發送的是序列化的ArrayList,接收也要用getSerializable
ArrayList<Student> s=(ArrayList<Student>) bundle.getSerializable("students");
//如果能成功接收到就顯示成功,如果能進行到這一行說明資料成功接收
Toast.makeText(Main2Activity.this,"成功",Toast.LENGTH_SHORT).show();
//用一個循環對數組進行指派
for(int i=0;i<s.size();i++){
String string=i+1+"、"+s.get(i).getName()+" "+"年齡:"+s.get(i).getAge()+" "+"性别:"+s.get(i).getSex();
str[i]=string;
}
//生成擴充卡
ArrayAdapter<String> adapter=new ArrayAdapter(Main2Activity.this,android.R.layout.simple_list_item_1,str);
ListView listView=findViewById(R.id.listview);
//最後進行适配就大功告成了
listView.setAdapter(adapter);
}catch (Exception e){
e.printStackTrace();
}
}
}
最終結果是進行多次送出然後資料全都記錄下來并顯示在ListView中
效果圖: