可点击的布局像Android闹钟?

时间:2012-05-30 13:16:06

标签: android

我有这样的布局:每行都是可点击的

---------------------------
  stuff from database
---------------------------
 next entry from database
---------------------------
next entry
--------------------------

我喜欢做的是:第一部分视图“来自数据库的东西”将有关于闹钟的信息,例如时间闹钟响起,星期几等等,它可以点击并打开视图的一部分也是可点击的,但应该能够从它旁边的行中获取信息,因为它需要信息说出报警ID我该怎么做?


 stuff from database       |  turn on
--------------------------------------
 next entry from database  | turn on
--------------------------------------
next entry                 | turn on
---------------------------------------

这是我的代码:

 protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);


        databaseHelper = new DatabaseStore(this);
        databaseHelper.open();
        ListViewData();

        registerForContextMenu(getListView());
    }


    private void ListViewData(){
         Cursor MainPageCursor = databaseHelper.fetchAllReminders();
         startManagingCursor(MainPageCursor);


         String[] empty = new String[] { DatabaseStore.MUTE_TITLE,  DatabaseStore.START_KEY_TIME, DatabaseStore.END_KEY_TIME, DatabaseStore.COVERT_DAYS }; 



            int[] notempty = new int[]{R.id.text1,R.id.text2, R.id.text4,  R.id.text5};
            SimpleCursorAdapter mainPageList = new SimpleCursorAdapter(this, R.layout.editinfo_row,MainPageCursor, empty, notempty);
           setListAdapter(mainPageList); 


          }

protected void onListItemClick(ListView l, View v, int position, long id){
    super.onListItemClick(l, v, position, id);
    Intent i = new Intent(this, AddTimeEntry.class); /// fix this back to wha tit was
    i.putExtra(DatabaseStore.kEY_ROWID, id);
     startActivityForResult(i, ACTIVITY_EDIT); 

}

editinfo_tow.xml

<?xml version="1.0" encoding="utf-8"?>

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/widget38"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginLeft="10dip"
        android:layout_marginRight="10dip"
        android:orientation="vertical" >

        <TextView
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:textSize="25sp" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >


            <TextView
                android:id="@+id/text2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="30sp" />
    <TextView
                android:id="@+id/text3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=" - "
                android:textSize="30sp" />
            <TextView
                android:id="@+id/text4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="30sp" />

             <CheckBox
                  android:text=""
        android:id="@+id/list_checkbox"
      android:layout_marginLeft="20dp"
       android:layout_width="wrap_content"
                android:layout_height="wrap_content"
        android:checked="false"
        android:focusable="false"
android:focusableInTouchMode="false"
        />

</LinearLayout>



  <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
            <TextView
                android:id="@+id/text5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="15sp" />

        </LinearLayout>

    </LinearLayout>

更新

public void addListenerOnButton() {

    final ToggleButton toggleButton1 = (ToggleButton) findViewById(R.id.toggleButton1);
    //toggleButton2 = (ToggleButton) findViewById(R.id.toggleButton2);
    Button btnDisplay = (Button) findViewById(R.id.toggleButton1);

    btnDisplay.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

          // StringBuffer result = new StringBuffer();
          // result.append("toggleButton1 : ").append(toggleButton1.getText());
    //     result.append("\ntoggleButton2 : ").append(toggleButton1.getText());

         //  Toast.makeText(MyAndroidAppActivity.this, result.toString(),
            //Toast.LENGTH_SHORT).show();

        }

    });

 }

我使用上面的代码,我得到空指针异常?

1 个答案:

答案 0 :(得分:0)

您可以按如下方式使用切换按钮:

<LinearLayout
                        android:id="@+id/prob1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" >

                        <TextView
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_weight="4"
                            android:gravity="left|center"
                            android:text="@string/someText"
                            android:textColor="@color/lbl_text_color"
                            android:textSize="25sp" >
                        </TextView>

                        <ToggleButton
                            android:id="@+id/toggle"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center|center_vertical"
                            android:layout_weight="1"
                            android:background="@drawable/toggle_no_img"
                            android:onClick="onToggleClick"
                            android:textOff=""
                            android:textOn="" />
                    </LinearLayout>