2015-10-13 39 views
-1

我希望開發具有文本視圖和幾個buttons.I已經把4個按鈕上我的應用程序WhatsApp的2和2個facebook.I一個簡單的Android應用程序訪問的WhatsApp和FB希望,每當一個人點擊在whatsapp按鈕上,文本視圖中的文本被轉發給whatsapp用戶想要發送消息的用戶的聯繫人。類似地,我希望該用戶能夠將fb中的私人消息發送給他的朋友。消息將與textview中的文本相同。我MainActivity.java文件低於無法從其他Android應用

package com.example.shalabh.gratbites; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.content.Intent; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

public class MainActivity extends AppCompatActivity { 

    public TextView textView; 
    public TextView textView2; 
    public Button button; 
    public Button button2; 
    public Button button3; 
    public Button button4; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

    } 

    public void onClickListenerButton2() 
    { 
     textView=(TextView)findViewById(R.id.textView); 
     button2=(Button)findViewById(R.id.button2); 
     button2.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       String fb = textView.getText().toString(); 
       Intent shareIntent = new Intent(); 
       shareIntent.setAction(Intent.ACTION_SEND); 
       shareIntent.setType("text/plain"); 
       shareIntent.putExtra(Intent.EXTRA_TEXT, fb); 
       startActivity(Intent.createChooser(shareIntent,"Share gratitude")); 

      } 
     }); 
    } 



    public void onClickListenerButton3() 
    { 
     textView2 = (TextView)findViewById(R.id.textView2); 
     button3= (Button)findViewById(R.id.button3); 
     button3.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       String whatsAppMessage = textView2.getText().toString(); 

       Intent sendIntent = new Intent(); 
       sendIntent.setAction(Intent.ACTION_SEND); 
       sendIntent.putExtra(Intent.EXTRA_TEXT, whatsAppMessage); 
       sendIntent.setType("text/plain"); 

       // Do not forget to add this to open whatsApp App specifically 
       sendIntent.setPackage("com.whatsapp"); 
       startActivity(sendIntent); 

      } 
     }); 


    } 

    public void onClickListenerButton() 
    { 
     textView= (TextView)findViewById(R.id.textView); 
     button= (Button)findViewById(R.id.button); 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       String whatsAppMessage = textView.getText().toString(); 

       Intent sendIntent = new Intent(); 
       sendIntent.setAction(Intent.ACTION_SEND); 
       sendIntent.putExtra(Intent.EXTRA_TEXT, whatsAppMessage); 
       sendIntent.setType("text/plain"); 

       // Do not forget to add this to open whatsApp App specifically 
       sendIntent.setPackage("com.whatsapp"); 
       startActivity(sendIntent); 

      } 
     }); 














    } 


} 

我main_activity.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:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" 
    android:background="#ffffff"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="At times our own light goes out and is rekindled by a spark from another person. Each of us has cause to think with deep gratitude of those who have lighted the flame within us. " 
     android:id="@+id/textView" 
     android:layout_alignParentTop="true" 
     android:layout_marginTop="38dp" 
     android:editable="false" 
     android:elegantTextHeight="false" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Whatsapp" 
     android:id="@+id/button" 
     android:layout_below="@+id/textView" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_marginTop="40dp" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/button2" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" 
     android:layout_alignRight="@id/button" 
     android:layout_alignTop="@+id/button" 
     android:text="Facebook" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="I wanted to say thanks... and share my gratitude for everything I&apos;ve been blessed with. Family, friends, and continued support from everyone." 
     android:id="@+id/textView2" 
     android:layout_below="@+id/button" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_marginTop="74dp" 
     android:layout_alignRight="@+id/button2" 
     android:layout_alignEnd="@+id/button2" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="whatsapp" 
     android:id="@+id/button3" 
     android:layout_below="@+id/textView2" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_marginTop="40dp" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Facebook" 
     android:id="@+id/button4" 
     android:layout_alignTop="@+id/button3" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" /> 


</RelativeLayout> 

問題是,所有的按鈕都沒有反應。我無法理解的原因。善意幫助我找到解決我的問題,如果可能的話建議替代方法。

P.S.我還沒有完成button4的編碼

回答

0

你還沒有調用你創建的方法。因此onClickListeners未設置。所以只需在onCreate中調用這個方法就可以了,你很好。

@Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     onClickListenerButton2(); 
     onClickListenerButton3(); 
     onClickListenerButton(); 
    }