【发布时间】:2014-09-21 13:02:24
【问题描述】:
所以我有这个输出:http://imgur.com/MM6QwgQ
它由顶部的 ToggleButton 和 TextView 表示。总的来说,我有 18 个按钮和 18 个文本视图。我想要做的是以某种方式将每个 TextView 链接到一个按钮,这样每当我按下一个按钮时,就可以从 textView 中获取文本,这样我就可以在另一个活动中显示它。这种工作方式就像预订一样。只需要一个小时。
为了生成 TextView 和 Button,我使用了一个适配器:
public class TimeActivity extends Activity {
private GridView gridView;
private final String[] items = new String[] {"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""};
private final String[] hours = new String[] {"06:00", "12:00", "18:00", "07:00", "13:00", "19:00", "08:00", "14:00", "20:00", "09:00", "15:00", "21:00", "10:00", "16:00", "22:00", "11:00", "17:00", "23:00"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_time);
setFonts();
gridView = (GridView) this.findViewById(R.id.timeInputView);
CustomGridAdapter adapter = new CustomGridAdapter(TimeActivity.this, items, hours);
gridView.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.time, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void setFonts() {
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Roboto-Medium.ttf");
TextView dateOutput = (TextView) findViewById(R.id.dateOutput);
dateOutput.setTypeface(tf);
}
}
适配器:
public class CustomGridAdapter extends BaseAdapter {
private Context _context;
private String[] _items;
private String[] _hours;
LayoutInflater _inflater;
public CustomGridAdapter(Context context, String[] items, String[] hours) {
this._context = context;
this._items = items;
this._hours = hours;
_inflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return _items.length;
}
@Override
public Object getItem(int position) {
return _items[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = _inflater.inflate(R.layout.screen_gridcell_time, null);
}
Button button = (Button) convertView.findViewById(R.id.calendar_day_gridcell);
button.setText(_items[position]);
TextView tv = (TextView) convertView.findViewById(R.id.top_time);
tv.setText(_hours[position]);
return convertView;
}
}
还有布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@android:color/transparent">
<TextView
android:id="@+id/top_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textSize="8dp"
android:textColor="@color/toggle_color"/>
<ToggleButton
android:id="@+id/calendar_day_gridcell"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:layout_below="@id/top_time"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/wampWhite"
android:background="@drawable/time_input_button_selector"
android:layout_marginLeft="2dp"
android:textOff=""
android:textOn="\u2713"
/>
</RelativeLayout>
【问题讨论】:
-
那么到底是什么问题呢?您需要为按钮编写 onClickListener !
-
好的...一旦单击按钮,我将如何从正确的 TextView 获取文本?请在我的实现之后给我看一些代码示例吗?