点击按钮在android中没有发生任何事情

时间:2014-07-30 07:24:53

标签: android

在我的应用程序中,我正在显示包含图像和文本的列表视图,我添加了一个名为read more的按钮,如果我点击了我想要移动到下一个活动的阅读更多按钮。

任何人都可以帮忙。

OurTeam课程

public class OurTeam extends Activity {
    Button click;

    // Array of strings storing country names
     String[] countries = new String[] {
                "Arun Arora Chairman, Edvance Group",
                "Anshul Arora CEO, Edvance Group ",
                "Ranjan Goyal CEO, Edvance Pre-Schools "

        };

    // Array of integers points to images stored in /res/drawable-ldpi/
     int[] flags = new int[]{
                R.drawable.arun_arora_chairman,
                R.drawable.anshul_arora_ceo,
                R.drawable.ranjan_goyal_ceo




        };

    // Array of strings to store currencies


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ourteam1);      

       click = (Button)findViewById(R.id.click);

        //click.setBackgroundColor(Color.TRANSPARENT);
        /*click.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //click.setVisibility(View.INVISIBLE);
                //rl.setBackgroundResource(R.drawable.voted);               
                //mp.start();       
                Intent in = new Intent(getApplicationContext(), AboutUs.class);
                startActivity(in);
                //text.setText("Thanks for Voting S K Vel Election Date is on 24th April 2014. Please come and vote for your S K Vel. Our Party Symbol is Drum.");
                //text.setVisibility(View.VISIBLE);
            }
        }); */

        // Each row in the list stores country name, currency and flag
        List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();        

        for(int i=0;i<3;i++){
            HashMap<String, String> hm = new HashMap<String,String>();
            hm.put("txt", countries[i]);

            hm.put("flag", Integer.toString(flags[i]) );            
            aList.add(hm);        
        }

        // Keys used in Hashmap
        String[] from = { "flag","txt"};

        // Ids of views in listview_layout
        int[] to = { R.id.flag,R.id.txt};        

        // Instantiating an adapter to store each items
        // R.layout.listview_layout defines the layout of each item
        SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.ourteam, from, to);


        // Getting a reference to listview of main.xml layout file
        final ListView listView = ( ListView ) findViewById(R.id.listview);

        // Setting the adapter to the listView
        listView.setAdapter(adapter);  

        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int position, long arg3) {

                switch (position) {
                case R.id.click:
                    Intent in = new Intent(getApplicationContext(), AboutUs.class);
                    startActivity(in);
                      break;

                }


            }


        });


    }



}

ourteam.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:background="@drawable/layout_bg"    
     >

    <ImageView 
            android:id="@+id/flag"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/hello"
            android:paddingTop="10dp"
            android:paddingRight="10dp"
            android:paddingBottom="10dp"        

            />

        <LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"            
            >    

            <TextView 
                android:id="@+id/txt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#000000"
                android:textSize="16sp" 
                android:textStyle="bold"            
            />   
             <Button
                android:id="@+id/click"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal"

                android:text="Read More" />       



        </LinearLayout>
</LinearLayout>

ourteam1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >



    <ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
         android:divider="@android:color/transparent"
        android:dividerHeight="10.0sp"

    />
</LinearLayout>

4 个答案:

答案 0 :(得分:0)

我认为您使用ListView的自定义适配器,然后在适配器getView()中使用Button OnClick ..

 holder.yourbutton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
                Intent in = new Intent(context, AboutUs.class);
                context.startActivity(in);
            }
         }

答案 1 :(得分:0)

您必须使用自定义适配器。

  1. 创建一个类并使用BaseAdapter进行扩展
  2. 在getView方法中,您必须添加该按钮的Click事件以及该按钮上的特定操作
  3. 在主Activity中设置Listview此适配器。

答案 2 :(得分:0)

您的问题出在onItemClick方法中。您可以阅读this以了解此方法中的参数。

您需要以不同方式检查位置:

 @Override
 public void onItemClick(AdapterView<?> arg0, View arg1,
          int position, long arg3) {

         switch (position) {
         case 0: // For "Arun Arora Chairman, Edvance Group"
             Intent in = new Intent(getApplicationContext(), AboutUs.class);
             startActivity(in);
             break;
         case 1: // For "Anshul Arora CEO, Edvance Group "
             Intent in = new Intent(getApplicationContext(), AboutUs.class);
             startActivity(in);
             break;
         ...
         }
}

如果要启动相同的活动,可以删除开关案例。

即使点击位于按钮之外,此方法也会触发onItemClick,如果您想将其限制为创建CustomAdapter所需的按钮。

答案 3 :(得分:0)

在代码中的此行click = (Button)findViewById(R.id.click);下面添加以下代码

click.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent in = new Intent(OurTeam.this, AboutUs.class);
            startActivity(in);

        }
    });