Android新增多個Button的正確方法
阿新 • • 發佈:2019-01-26
作為初學者的我們,真的能幹出很有意思的事,比如這樣一個介面
三八二十四個按鈕,然後每一個都有點選事件,我是怎麼實現的、看下:
就這樣,一個TableLayout,一行三個,一共八行然後每個Buttton手動新增名字,點選事件,就這個xml就有300多行,真佩服!
<TableRow> <Button android:id="@+id/beijing_button" android:layout_width="0dp" android:layout_height="35dp" android:layout_weight="1" android:background="@drawable/button_add_city_selector" android:layout_marginLeft="8dp" android:layout_marginTop="8dp" android:text="北京" android:onClick="click"/> <Button android:id="@+id/shanghai_button" android:layout_width="0dp" android:layout_height="35dp" android:layout_weight="1" android:background="@drawable/button_add_city_selector" android:layout_marginLeft="8dp" android:layout_marginTop="8dp" android:text="上海" android:onClick="click"/> <Button android:id="@+id/guangzhou_button" android:layout_width="0dp" android:layout_height="35dp" android:layout_weight="1" android:background="@drawable/button_add_city_selector" android:layout_marginLeft="8dp" android:layout_marginTop="8dp" android:text="廣州" android:onClick="click"/> </TableRow> <TableRow> <Button android:id="@+id/shenzhen_button" android:layout_width="0dp" android:layout_height="35dp" android:layout_weight="1" android:background="@drawable/button_add_city_selector" android:layout_marginLeft="8dp" android:layout_marginTop="8dp" android:text="深圳" android:onClick="click"/> <Button android:id="@+id/nanjing_button" android:layout_width="0dp" android:layout_height="35dp" android:layout_weight="1" android:background="@drawable/button_add_city_selector" android:layout_marginLeft="8dp" android:layout_marginTop="8dp" android:text="南京" android:onClick="click"/> <Button android:id="@+id/hangzhou_button" android:layout_width="0dp" android:layout_height="35dp" android:layout_weight="1" android:background="@drawable/button_add_city_selector" android:layout_marginLeft="8dp" android:layout_marginTop="8dp" android:text="杭州" android:onClick="click"/> </TableRow>
再給你們看下Activity中的點選事件,哈哈哈
都是可以理解的,初學者的執著,那今天有時間了,我把他正常的寫一下吧:public void click(View v) { // TODO Auto-generated method stub //獲取元件的資源id int id = v.getId(); switch (id) { case R.id.beijing_button: city = "北京"; break; case R.id.shanghai_button: city = "上海"; break; case R.id.guangzhou_button: city = "廣州"; break; case R.id.shenzhen_button: city = "深圳"; break; case R.id.nanjing_button: city = "南京"; break; case R.id.hangzhou_button: city = "杭州"; break; case R.id.suzhou_button: city = "蘇州"; break; case R.id.qingdao_button: city = "青島"; break; case R.id.jinan_button: city = "濟南"; break; case R.id.zhengzhou_button: city = "鄭州"; break; case R.id.xian_button: city = "西安"; break; case R.id.shenyang_button: city = "瀋陽"; break; case R.id.changsha_button: city = "長沙"; break; case R.id.wuhan_button: city = "武漢"; break; case R.id.xiamen_button: city = "廈門"; break; case R.id.chengdu_button: city = "成都"; break; case R.id.chongqing_button: city = "重慶"; break; case R.id.changchun_button: city = "長春"; break; case R.id.sanya_button: city = "三亞"; break; case R.id.zhuhai_button: city = "珠海"; break; case R.id.tianjin_button: city = "天津"; break; case R.id.taiyuan_button: city = "太原"; break; case R.id.datong_button: city = "大同"; break; case R.id.yanggao_button: city = "陽高"; break; default: break; }
首先在專案中新增assets資料夾,新建一個txt檔案,裡面放好我們寫好的JSON資料(這個得自己搞啦)
[ { "id": "1", "cityName": "北京" }, { "id": "2", "cityName": "上海" }, { "id": "3", "cityName": "廣州" }, { "id": "4", "cityName": "深圳" }, { "id": "5", "cityName": "南京" }, { "id": "6", "cityName": "杭州" }, { "id": "7", "cityName": "蘇州" }, { "id": "8", "cityName": "青島" }, { "id": "9", "cityName": "濟南" }, { "id": "10", "cityName": "鄭州" }, { "id": "11", "cityName": "西安" }, { "id": "12", "cityName": "瀋陽" }, { "id": "13", "cityName": "長沙" }, { "id": "14", "cityName": "武漢" }, { "id": "15", "cityName": "廈門" }, { "id": "16", "cityName": "成都" }, { "id": "17", "cityName": "重慶" }, { "id": "18", "cityName": "長春" }, { "id": "19", "cityName": "三亞" }, { "id": "20", "cityName": "珠海" }, { "id": "21", "cityName": "天津" }, { "id": "22", "cityName": "太原" }, { "id": "23", "cityName": "大同" }, { "id": "24", "cityName": "陽高" } ]
解析JSON新建的JavaBean類
public class PopularCity {
/**
* id : 1
* cityName : 北京
*/
private String id;
private String cityName;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
}
在activity中自定義一個方法,用來獲取JSON資料(我們這小Demo,就寫activity裡吧,正常開發都得放到工具類裡面的)
/**
* 從assets檔案中獲得Json串
*/
private String getAssetsData(String fileName) {
try {
InputStreamReader inputReader = new InputStreamReader(getResources().getAssets().open(fileName));
BufferedReader bufReader = new BufferedReader(inputReader);
String line = "";
String Result = "";
while ((line = bufReader.readLine()) != null) {
Result += line;
}
return Result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
佈局檔案中新增GridView
<GridView
android:id="@+id/grid_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="3"
android:listSelector="@android:color/transparent">
</GridView>
GridView的Item
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/small_textview"
android:layout_width="match_parent"
android:layout_height="35dp"
android:background="@drawable/button_add_city_selector"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_centerInParent="true"
android:gravity="center" />
</RelativeLayout>
GridView的介面卡
public class GridViewAdapter extends BaseAdapter {
private Context context;
private List<PopularCity> popularCityList;
public GridViewAdapter(Context context, List<PopularCity> popularCity) {
this.context = context;
this.popularCityList = popularCity;
}
@Override
public int getCount() {
return popularCityList.size();
}
@Override
public Object getItem(int position) {
return popularCityList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.gridview_list_item, null);
holder = new ViewHolder();
holder.textView = (TextView) convertView.findViewById(R.id.small_textview);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText(popularCityList.get(position).getCityName());
return convertView;
}
public static class ViewHolder {
TextView textView;
}
}
Activity中獲取GridView的例項,設定介面卡,點選事件
gridView = (GridView) findViewById(R.id.grid_view);
popularCityList = new Gson().fromJson(getAssetsData("cities.txt"), new TypeToken<List<PopularCity>>() {
}.getType());
GridViewAdapter adapter = new GridViewAdapter(this, popularCityList);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(CityAddActivity.this,popularCityList.get(i).getCityName(),Toast.LENGTH_SHORT).show();
}
});
總結
哈哈,不知不覺寫了一堆東西,亂七八糟的,就是想告訴像我一樣的初級選手,不要傻乎乎的寫重複的東西,一點效率都沒有,大家都是慢慢成長起來的,希望我的一些東西可以分享給你們,加油吧!