将TextViews链接到Android按钮

时间:2014-09-21 13:02:24

标签: java android

所以我有这个输出:http://imgur.com/MM6QwgQ

由顶部的ToggleButton和TextView表示。总的来说,我有18个按钮和18个textViews。我想要做的是以某种方式将每个TextView链接到一个Button,这样每当我按下一个按钮,就可以从textView中获取Text,这样我就可以在另一个活动中显示它。这种方式的工作方式就像预订一样。它只需要一个小时。

为了生成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>

1 个答案:

答案 0 :(得分:1)

您可以在hashmap中链接它们。

类似

假设您有一个按钮aButton和一个textview aTextView

HashMap<Button,TextView> myMap = new HashMap<Button,TextView>();

myMap.put(aButton, aTextView);

然后使用

获取它们
myMap.get(aButton);