編輯:關於Android編程
package com.example.arraylist;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity extends
Activity {
private static final String[] places = {"銅鼓","大段","古橋"};
TextView mTextView;
EditText mEditText;
Button mButton1, mButton2;
Spinner spinner;
ArrayAdapter<String> adapter1;
ArrayList<String> list1;
@Override
public void onCreate(
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list1=new ArrayList<String>();
for (int i = 0; i < places.length; i++) {
list1.add(places[i]);
}
adapter1=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list1);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mTextView=(TextView)findViewById(R.id.textView1);
mEditText=(EditText)findViewById(R.id.editText1);
mButton1=(Button)findViewById(R.id.button1);
mButton2=(Button)findViewById(R.id.button2);
spinner=(Spinner)findViewById(R.id.spinner1);
spinner.setAdapter(adapter1);
mButton1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String newplace=mEditText.getText().toString();
for (int i = 0; i < adapter1.getCount(); i++) {
if (newplace.equals(adapter1.getItem(i))) {
return;
}
}
if (!newplace.equals("")) {
adapter1.add(newplace);
int position=adapter1.getPosition(newplace);
spinner.setSelection(position);
mEditText.setText("");
}
}
});
mButton2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if (spinner.getSelectedItem()!=null) {
adapter1.remove(spinner.getSelectedItem().toString());
mEditText.setText("");
if (adapter1.getCount()==0) {
mTextView.setText("");
}
}
}
});
spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
public void onItemSelected(
AdapterView<?> arg0,
View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
mTextView.setText(arg0.getSelectedItem().toString());
}
public void onNothingSelected(
AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
@Override
public boolean onCreateOptionsMenu(
Menu menu) {
getMenuInflater().inflate(
R.menu.activity_main,
menu);
return true;
}
}
進程間通信方式在Android開發中我們可以通過Intent、ContentProviders來實現進程間通信,如果不限於Android特有的話,我們還可以使用File、
天哪,這篇文章終於說道如何自定義權限了,左盼右盼,其實這個自定義權限相當easy。為了方便敘述,我這邊會用到兩個app作為例子示范。Permission App: use
自己寫的安卓的計算器:注:這個是在mac中開發的,如果要在windows的eclipse中運行可能會出現路徑問題,解決辦法從windows中已有的安卓工程根目錄下復制一下
UIPickerView功能與UIDatePicker類似初始化實例時,通常只需要設置原點坐標,不需要設置寬高(默認寬高為:frame = (0 0; 320 216))