2014-09-21 54 views
0

所以我有這樣的輸出:http://imgur.com/MM6QwgQ鏈接TextViews到按鈕的Android

這是由一個切換按鈕,並在上面一個TextView表示。總的來說,我有18個按鈕和18個文本視圖。我試圖做的是以某種方式將每個TextView鏈接到一個按鈕上,只要按下一個按鈕,就可以從textView中獲取文本,以便我可以在另一個活動中顯示它。這種工作方式就像預訂。這只是一小時。

爲了產生的TextView和按鈕,我使用的適配器:

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> 
+0

那麼究竟是什麼問題呢?你需要編寫按鈕的onClickListener! – 2014-09-21 13:53:09

+0

好的......一旦按鈕被點擊,我將如何從正確的TextView中獲取文本?請問我可以在執行後向我展示一些代碼示例嗎? – funkycookie 2014-09-21 14:02:47

回答

1

你可以將它們鏈接到一個hashmap中。

因此,像

假設你有一個按鈕,A按鈕和一個TextView aTextView

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

myMap.put(aButton, aTextView); 

然後讓他們用

myMap.get(aButton); 
+0

好主意,但不知道我應該如何在HashMap中添加按鈕和文字瀏覽,以及我的實現.. – funkycookie 2014-09-22 08:46:04

+0

您是否對如何使用適配器感到困惑? – nPn 2014-09-22 13:01:35

+0

爲解決方案而歡呼:)設法實現它,它工作正常! – funkycookie 2014-09-22 13:46:27