线性布局按钮单击不起作用

时间:2015-07-20 04:26:10

标签: android android-layout android-linearlayout onclicklistener

我实现了一个相对布局,它具有线性布局和列表视图。线性布局包含图像和按钮。是否有人可以帮助我?点击两个人的监听器都不起作用。

layout design

这是activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:paddingBottom="@dimen/activity_vertical_margin"  
tools:context=".MainActivity">

<LinearLayout
android:layout_width="fill_parent"
android:paddingTop="0dp"
android:id="@+id/ll"
android:layout_height="50dp"

android:background="#00897B"
android:orientation="horizontal"
android:weightSum="2"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true">
<Button
    style="?android:attr/buttonStyleSmall"
    android:background="#00897B"
    android:layout_width="180dp"

    android:layout_height="wrap_content"
    android:textStyle="bold"
    android:layout_weight="1.9"
    android:textAllCaps="false"
   android:layout_gravity="center_horizontal"
    android:textSize="16dp"
    android:textColor="#ffffff"
    android:text="Register an event"
    android:id="@+id/button"
    android:layout_alignTop="@+id/listView"
    android:layout_alignParentStart="true" />
<ImageView
    android:layout_width="20dp"
    android:layout_height="30dp"
    android:layout_weight="0.1"
    android:layout_marginTop="10dp"
    android:background="@drawable/refr"
    android:id="@+id/refresh"
    android:onClick="ref"
    android:layout_alignParentEnd="true"

    android:layout_marginEnd="28dp"
    android:layout_alignBottom="@+id/ll"
    android:layout_alignParentTop="true" />
</LinearLayout>
<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/listView"
    android:paddingTop="60dp"
    android:layout_below="@+id/ll"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:divider="#2196f3"
    android:dividerHeight="3dp"
    android:scrollbars="none"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />
 </RelativeLayout>

这是MainActivity.java

public class MainActivity extends Activity  {

ListView lv;
Button bt1;
TextView tv1,tv2,tv3;
ArrayList<String> a=new ArrayList<String>();
ArrayList<String> b=new ArrayList<String>();
ArrayList<String> c=new ArrayList<String>();
ArrayList<String> d=new ArrayList<String>();
String mydata,name,name1,society,date,venue;
public String[] s1 = new String[50];
public String[] s2=new String[50];
public String[] s3=new String[50];
public String[] s4=new String[50];
public int[] img = {R.drawable.rty, R.drawable.sf, R.drawable.rty};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tv1=(TextView)findViewById(R.id.textView);
    lv = (ListView) findViewById(R.id.listView);
    Button bt1=(Button)findViewById(R.id.button);
    bt1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent in=new Intent(MainActivity.this,webform.class);
            startActivity(in);
        }
    });
    ConnectionDetector cd = new ConnectionDetector(getApplicationContext());
    Boolean isInternetPresent = cd.isConnectingToInternet(); // true or false

    if(isInternetPresent) {
        new MyData().execute();
    }
    else
    Toast.makeText(MainActivity.this,"No Internet Connection",Toast.LENGTH_SHORT).show();
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Intent in = new Intent(MainActivity.this, listclick.class);
            in.putExtra("position", position);
            startActivity(in);

        }
    });
}




public void ref(View v)
{
    ConnectionDetector cd = new ConnectionDetector(getApplicationContext());
    Boolean isInternetPresent = cd.isConnectingToInternet();
    if(isInternetPresent) {



        new MyData().execute();


    }
    else
        Toast.makeText(MainActivity.this,"No Internet Connection",Toast.LENGTH_SHORT).show();

}
public class MyData extends AsyncTask<String,String,String>
{
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        CustomAdapter cad = new CustomAdapter(MainActivity.this, s1, img,s2,s3,s4);
        lv.setAdapter(cad);
        cad.notifyDataSetChanged();
    }

    @Override
    protected String doInBackground(String... params) {
        getData();
        return null;
    }
}

public void getData()
{
    try {
        HttpClient httpClient=new DefaultHttpClient();

        HttpPost httpPost=new HttpPost("http://collegeevents.esy.es/abc.php");
        HttpResponse response=httpClient.execute(httpPost);
        HttpEntity httpEntity=response.getEntity();
        InputStream is=httpEntity.getContent();
        BufferedReader reader=new BufferedReader(new InputStreamReader(is,"utf-8"),8);
        StringBuilder strbuilder=new StringBuilder();
        String line=null;
        while ((line=reader.readLine())!=null)
        {
            strbuilder.append(line);
        }
        is.close();
        mydata=strbuilder.toString();
        JSONArray obj=new JSONArray(mydata);
        a.clear();
        b.clear();
        c.clear();
        d.clear();
        for(int i=0;i<obj.length();i++)
        {

            JSONObject obj1=obj.getJSONObject(i);

            a.add(i,obj1.getString("Name"));
            b.add(i,obj1.getString("society"));
            c.add(i,obj1.getString("venue"));
            d.add(i,obj1.getString("date"));
        }
        String[] s = new String[a.size()];
        s=a.toArray(s);
        s1 = s;

        String[] soc = new String[b.size()];
        soc=b.toArray(soc);
        s2 = soc;

        String[] ven = new String[c.size()];
        ven=c.toArray(ven);
        s3 = ven;

        String[] dat = new String[d.size()];
        dat=d.toArray(dat);
        s4 = dat;
    }
    catch (Exception e)
    {

    }


}


}

2 个答案:

答案 0 :(得分:0)

您添加了按钮clickable=false的属性,这就是为什么它不起作用从您的按钮中移除相同的属性它将正常工作。

<Button
    style="?android:attr/buttonStyleSmall"
    android:background="#00897B"
    android:layout_width="180dp"
    android:layout_height="wrap_content"
    android:textStyle="bold"
    android:layout_weight="1.9"
    android:textAllCaps="false"
   android:layout_gravity="center_horizontal"
    android:textSize="16dp"
    android:textColor="#ffffff"
    android:text="Register an event"
    android:id="@+id/button"
    android:layout_alignTop="@+id/listView"
    android:layout_alignParentStart="true" />

答案 1 :(得分:0)

它们都在XML文件中设置为clickable = false

另一件事是你有许多矛盾或无用的XML属性。对于你的列表视图中的ex,你有

android:layout_below="@+id/ll"
android:layout_alignParentTop="true"

这没有任何意义。也许您的列表视图覆盖了Linearlayout,阻止了点击次数达到它。这真是一个猜测。

无论如何尝试删除所有不必要的属性,这肯定会有所帮助。