2016-02-02 9 views
-1

У меня есть несколько кнопок в действии, но проблема в том, что некоторые кнопки были нажаты, а некоторые не были нажаты, а setOnClickListener.Button click not working android

Это моя важная часть проекта, и я очень хочу это сделать. но я не получаю успеха. поэтому, пожалуйста, помогите мне. Я проверил несколько решений с другого сайта, но не работал.

Это часть кода активности.

public class Home extends ActionBarActivity { 

    String htmlText = "<html><body style=\"text-align:justify\"> %s </body></Html>"; 

    //HOME SECTION 
    TextView txt1,txt2,txt3,txt4,txt5,txt6; 
    WebView webview1,webview2,webview3; 
    WebSettings websetting; 

    Button btnhowtouse,btnteacher, btn_addlocation, btn_student_parents; 


    Dialog dialog; 
    TextView txt_howtouse,txtdialog1,txtdialog2,txtdialog3,txtdialog4,txtdialog5,txtdialog6,txtdialog7,txtdialog8,txtdialog9,txtdialog10,txtdialog11, 
    txtdialog12,txtdialog13,txtdialog14,txtdialog15,txtdialog16,txtdialog17,txtdialog18,txtdialog19,txtdialog20,txtdialog21,txtdialog22,txtdialog23; 

    ImageView imgcross; 

    Toolbar toolbar; 

    int groupid; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_usecoop); 


     toolbar=(Toolbar)findViewById(R.id.toolbar); 

     try { 

      setSupportActionBar(toolbar); 
      getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
      getSupportActionBar().setDisplayShowHomeEnabled(true); 


     } catch (Exception e) { 
     } 


     btnhowtouse=(Button)findViewById(R.id.btn_howtouse); 
     btnteacher=(Button)findViewById(R.id.btn_teacherlocator); 
     btn_addlocation=(Button)findViewById(R.id.btn_addlocation); 
     btn_student_parents=(Button)findViewById(R.id.btn_student_parents); 




     Intent it=getIntent(); 
     groupid=it.getIntExtra("Groupid", 0); 

     Toast.makeText(Home.this, "Goupid"+groupid, Toast.LENGTH_LONG).show(); 


     if(groupid==4) 
     { 

      btnteacher.setVisibility(View.VISIBLE); 
      btn_student_parents.setVisibility(View.GONE); 
      btn_addlocation.setVisibility(View.GONE); 
     } 


     else if(groupid==3) 
     { 
      btn_student_parents.setVisibility(View.VISIBLE); 
      btnteacher.setVisibility(View.GONE); 
      btn_addlocation.setVisibility(View.GONE); 


     } 

     else 
     { 



      btn_addlocation.setVisibility(View.VISIBLE); 
      btnteacher.setVisibility(View.GONE); 
      btn_student_parents.setVisibility(View.GONE); 

     } 



     btn_student_parents.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       Toast.makeText(Home.this, "Clicked Student parents", Toast.LENGTH_LONG).show(); 
       Intent it=new Intent(Home.this,TeacherLocator.class); 
       startActivity(it); 

      } 
     }); 






      btnteacher.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Intent it=new Intent(Home.this,TeacherLocator.class); 
       startActivity(it); 

      } 
     }); 






     btn_addlocation.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 


       Toast.makeText(Home.this, "Currently you are not able to add location", Toast.LENGTH_LONG).show(); 

      } 
     }); 



     txt1=(TextView)findViewById(R.id.txt_home1); 
     txt2=(TextView)findViewById(R.id.txt_home2); 
     txt3=(TextView)findViewById(R.id.txt_home3); 
     txt4=(TextView)findViewById(R.id.txt_home4); 
     txt5=(TextView)findViewById(R.id.txt_home5); 
     txt6=(TextView)findViewById(R.id.txt_home6); 

     webview1=(WebView)findViewById(R.id.web_view1); 
     webview2=(WebView)findViewById(R.id.web_view2); 
     webview3=(WebView)findViewById(R.id.web_view3); 

     txt2.setText("Text1"); 
     txt4.setText("Text2"); 
     txt6.setText("Text3"); 

     websetting=webview1.getSettings(); 
     websetting=webview2.getSettings(); 
     websetting=webview3.getSettings(); 





     webview1.loadData(String.format(htmlText, txt2.getText()), "text/html", "utf-8"); 
     webview2.loadData(String.format(htmlText, txt4.getText()), "text/html", "utf-8"); 
     webview3.loadData(String.format(htmlText, txt6.getText()), "text/html", "utf-8"); 







     btnhowtouse.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       dialog=new Dialog(Home.this); 
       dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
       dialog.setContentView(R.layout.dilogusecoop); 
       dialog.setCancelable(true); 
       dialog.show();   

       imgcross=(ImageView)dialog.findViewById(R.id.img_corss); 
       imgcross.setOnClickListener(new OnClickListener() { 

        @Override 
        public void onClick(View v) { 
         dialog.dismiss(); 
        } 
       }); 

       txt_howtouse=(TextView)dialog.findViewById(R.id.txt_howtouse); 
       txtdialog1=(TextView)dialog.findViewById(R.id.txt_1); 
       txtdialog2=(TextView)dialog.findViewById(R.id.txt_2); 
       txtdialog3=(TextView)dialog.findViewById(R.id.txt_3); 
       txtdialog4=(TextView)dialog.findViewById(R.id.txt_4); 
       txtdialog5=(TextView)dialog.findViewById(R.id.txt_5); 
       txtdialog6=(TextView)dialog.findViewById(R.id.txt_6); 
       txtdialog7=(TextView)dialog.findViewById(R.id.txt_7); 
       txtdialog8=(TextView)dialog.findViewById(R.id.txt_8); 
       txtdialog9=(TextView)dialog.findViewById(R.id.txt_9); 
       txtdialog10=(TextView)dialog.findViewById(R.id.txt_10); 
       txtdialog11=(TextView)dialog.findViewById(R.id.txt_11); 
       txtdialog12=(TextView)dialog.findViewById(R.id.txt_12); 
       txtdialog13=(TextView)dialog.findViewById(R.id.txt_13); 
       txtdialog14=(TextView)dialog.findViewById(R.id.txt_14); 
       txtdialog15=(TextView)dialog.findViewById(R.id.txt_15); 
       txtdialog16=(TextView)dialog.findViewById(R.id.txt_16); 
       txtdialog17=(TextView)dialog.findViewById(R.id.txt_17); 
       txtdialog18=(TextView)dialog.findViewById(R.id.txt_18); 
       txtdialog19=(TextView)dialog.findViewById(R.id.txt_19); 
       txtdialog20=(TextView)dialog.findViewById(R.id.txt_20); 
       txtdialog21=(TextView)dialog.findViewById(R.id.txt_21); 
       txtdialog22=(TextView)dialog.findViewById(R.id.txt_22); 
       txtdialog23=(TextView)dialog.findViewById(R.id.txt_23); 
      } 
     }); 





    } 


} 

This is my layout please help me find out this problem. 





<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" 


    > 

    <include 
      android:id="@+id/toolbar" 
      layout="@layout/toolbar" /> 


<Button 
     android:id="@+id/btn_howtouse" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_marginBottom="5dp" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" 
     android:background="#4385F5" 
     android:textColor="#ffffff" 
     android:text="How To Locator" /> 

    <Button 
     android:id="@+id/btn_teacherlocator" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" 
     android:layout_marginBottom="5dp" 
     android:background="#4385F5" 
     android:textColor="#ffffff" 
     android:text="Teacher Locator" 
     android:layout_above="@+id/btn_howtouse" /> 



    <Button 
     android:id="@+id/btn_student_parents" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" 
     android:layout_marginBottom="5dp" 
     android:background="#4385F5" 
     android:textColor="#ffffff" 
     android:text="Parents/Students Locator" 
     android:visibility="gone" 

     android:layout_above="@+id/btn_howtouse" /> 



    <Button 
     android:id="@+id/btn_addlocation" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" 
     android:layout_marginBottom="5dp" 
     android:background="#4385F5" 
     android:textColor="#ffffff" 
     android:text="Add Location" 
     android:visibility="gone" 

     android:layout_above="@+id/btn_howtouse" /> 


    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginBottom="5dp" 
     android:layout_below="@+id/toolbar" 
     android:layout_above="@+id/btn_teacherlocator" > 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="#ffffff" > 

      <TextView 
       android:id="@+id/txt_home1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="5dp" 
       android:layout_marginTop="5dp" 
       android:text="Home" 
       android:textSize="25sp" 
       android:textStyle="bold" /> 

      <WebView 
       android:id="@+id/web_view1" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/txt_home1" 
       android:layout_marginLeft="5dp" 
       android:layout_marginRight="5dp" 
       android:layout_marginTop="5dp" /> 

      <TextView 
       android:id="@+id/txt_home2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
        android:layout_below="@+id/txt_home1" 
       /> 

      <TextView 
       android:id="@+id/txt_home3" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/web_view1" 
       android:layout_marginLeft="5dp" 
       android:layout_marginTop="5dp" 
       android:textColor="#000000" 
       android:textStyle="bold" 
       android:textSize="20sp" 
       android:text="What is Education?" /> 

      <WebView 
       android:id="@+id/web_view2" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/txt_home3" 
       android:layout_marginLeft="5dp" 
       android:layout_marginRight="5dp" 
       android:layout_marginTop="5dp" /> 

      <TextView 
       android:id="@+id/txt_home4" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 

       android:layout_below="@+id/txt_home3" 
      /> 

      <TextView 
       android:id="@+id/txt_home5" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/web_view2" 
       android:layout_marginLeft="5dp" 
       android:layout_marginTop="5dp" 
       android:textColor="#000000" 
       android:textStyle="bold" 
       android:textSize="20sp" 
       android:text="What are the benefits of Education?" /> 

      <WebView 
       android:id="@+id/web_view3" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/txt_home5" 
       android:layout_marginLeft="5dp" 
       android:layout_marginRight="5dp" 
       android:layout_marginTop="5dp" /> 

      <TextView 
       android:id="@+id/txt_home6" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/txt_home5" /> 
     </RelativeLayout> 
    </ScrollView> 


</RelativeLayout> 
+0

код выглядит хорошо на первый взгляд ... что кнопка проблематично? – webo80

+0

@ webo80 Я думаю, проблема для 'View.GONE' –

+0

#nanoweb Вы еще не решили? –

ответ

1

Advice

Bad Approach : Использование осуждаетсяActionBarActivity .Использование AppCompatActivity

Просто установите OnClick в XML, это гораздо проще.

android:onClick="Button_Click" 

Затем добавить в своем классе,

public void Button_Click(View v) { 
    // Add your Code 
} 

Наконец

<Button 
    android:id="@+id/btn_student_parents" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="5dp" 
    android:layout_marginRight="5dp" 
    android:layout_marginBottom="5dp" 
    android:background="#4385F5" 
    android:textColor="#ffffff" 
    android:text="Parents/Students Locator" 
    android:onClick="Button_Click" 
    android:layout_above="@+id/btn_howtouse" />