2016-11-23 76 views
1

我有2個按鈕和2個獨立的活動,我想是這樣控制流程:OnClickListener既不正確初始化,也不如預期工作

  • Activityone
  • buttonOneActivityTwo
  • buttonTwoActivityThree

main.xml:

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

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

    <LinearLayout 
     android:layout_height="50dp" 
     android:layout_width="match_parent" 
     android:orientation="horizontal"> 

     <Button 
      android:layout_height="match_parent" 
      android:layout_width="50dp" 
      android:background="@drawable/rozmare"/> 

     <TextView 
      android:layout_height="match_parent" 
      android:text="کار های روزمره" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:layout_width="match_parent" 
      android:gravity="center" 
      android:textSize="25sp"/> 

    </LinearLayout> 

    <EditText 
     android:textSize="20.0sp" 
     android:gravity="top|right" 
     android:id="@+id/editText1" 
     android:padding="5.0dip" 
     android:scrollbars="vertical" 
     android:fadingEdge="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" 
     android:text="" 
     android:capitalize="sentences" 
     android:layout_gravity="top|right" 
     android:textColor="#000000" 
     android:background="#FFFFFF"/> 

</LinearLayout> 

<LinearLayout 
    android:layout_height="match_parent" 
    android:layout_width="65dp" 
    android:orientation="vertical" 
    android:layout_margin="1dp"> 

    <ScrollView 
     android:layout_height="match_parent" 
     android:layout_width="match_parent"> 

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

      <Button 
       android:layout_height="65dp" 
       android:layout_width="match_parent" 
       android:id="@+id/notelist" 
       android:background="@drawable/personal" 
       android:layout_margin="1dp"/> 

      <Button 
       android:layout_height="65dp" 
       android:layout_width="match_parent" 
       android:background="@drawable/kasbokar" 
       android:layout_margin="1dp" 
       android:id="@+id/notelist1"/> 

     </LinearLayout> 

    </ScrollView> 

</LinearLayout> 

</LinearLayout> 

MainActivity.java

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.widget.Button; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.Menu; 
import android.widget.TextView; 
import android.widget.EditText; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 

public class MainActivity extends Activity { 

Button buttonOne; 
Button buttonTwo; 
EditText editText1; 
String fileName; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    addListenerOnButton(); 
    getActionBar().hide(); 
    getWindow().setSoftInputMode(3); 
    editText1 = (EditText) findViewById(R.id.editText1); 
    fileName = getResources().getString(R.string.file_name); 
    try { 
     FileInputStream fis = openFileInput(fileName); 
     byte[] readBytes = new byte[fis.available()]; 
     fis.read(readBytes); 
     editText1.setText(new String(readBytes)); 
     fis.close(); 
    } catch (Exception e) { 
     return; 
    } 
} 

public void onPause() { 
    super.onPause(); 
    try { 
     FileOutputStream fos = openFileOutput(fileName, 0); 
     fos.write(editText1.getText().toString().getBytes()); 
     fos.close(); 
    } catch (Exception e) { 
     return; 
    } 
} 

public void addListenerOnButton() { 

    final Context context = this; 

    buttonOne = (Button) findViewById(R.id.notelist); 

    buttonOne.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 

       Intent intent = new Intent(context, NoteList.class); 
       startActivity(intent); 


    buttonTwo = (Button) findViewById(R.id.notelist1); 

    buttonTwo.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 

      Intent intent = new Intent(context, NoteList1.class); 
      startActivity(intent); 

      } 

     }); 

} 



});} 

public void EXIT(View view) 
{ 
    finish(); 
}} 

的manifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="ir.whitegate.noteking" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="16" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 

    </activity> 
    <activity 
     android:name=".NoteList" 
     android:label="@string/app_name" 
     android:windowSoftInputMode="adjustUnspecified"/> 

    <activity 
     android:name=".NoteEdit" 
     android:label="@string/app_name" 
     android:windowSoftInputMode="adjustUnspecified"/> 

    <activity 
     android:name=".NoteList1" 
     android:label="@string/app_name" 
     android:windowSoftInputMode="adjustUnspecified"/> 

    <activity 
     android:name=".NoteEdit1" 
     android:label="@string/app_name" 
     android:windowSoftInputMode="adjustUnspecified"/> 
</application> 

但這兩個按鈕都做同樣的動作,他們都打算activityTwo和其它問題是,如果我不碰ButtonOneButtonTwo將無法​​正常工作。

+0

我看到現有答案沒有錯,如果你得到錯誤,[編輯]你的問題 –

回答

2

您已經添加了第二個按鈕的監聽器第一個按鈕的監聽器裏這樣

如果我不碰ButtonOne的ButtonTwo將無法正常工作。

  • 監聽第二個按鈕只有當你按下第一個按鈕

    初始化,但他們應該是獨立的

    public void addListenerOnButton() { 
        final Context context = this; 
        buttonOne = (Button) findViewById(R.id.notelist); 
    
        buttonOne.setOnClickListener(new OnClickListener() { 
         @Override 
         public void onClick(View arg0) { 
          Intent intent = new Intent(context, NoteList.class); 
          startActivity(intent); 
          } 
         }); 
    
        buttonTwo = (Button) findViewById(R.id.notelist1); 
    
        buttonTwo.setOnClickListener(new OnClickListener() {  
         @Override 
         public void onClick(View arg0) { 
          Intent intent = new Intent(context, NoteList1.class); 
          startActivity(intent);  
          }  
         });  
    } 
    

兩者要activityTwo

此行爲與簡單地使用相同的佈局結構有關,因此請使用settext函數或對XML代碼進行更改。

+0

我不能有兩個addlistener它不可能它說addlistener是al準備添加在代碼 –

+0

我怎麼能有兩個添加監聽器? –

+0

你可以在代碼中顯示它,請給我一個很大的幫助。 –

0

一個解決您的問題將是你的XML來定義的onClick功能

<Button 
android:text="Don't have an account? Join!" 
android:onClick="toRegister" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
/> 

然後在你的類

public void toRegister(View view){ 

    Intent newIntent = new Intent(this, SignUpActivity.class); 
    newIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    LoginActivity.this.startActivity(newIntent); 
} 
+0

儘管如此,但使用Java來做這件事更明確,而且您不會因爲不實施該方法而出現錯誤。所以,不確定這是否是簡單的解決方案 –

0

您已經在第一個監聽添加第二個偵聽定義功能。

buttonOne。setOnClickListener(新OnClickListener(){

@Override 
public void onClick(View arg0) { 

    Intent intent = new Intent(context, NoteList.class); 
    startActivity(intent); 


    buttonTwo = (Button) findViewById(R.id.notelist1); 

    // >>>>>>>>>>>>>>>>>>>>>>> inside buttonOne 
    buttonTwo.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 

      Intent intent = new Intent(context, NoteList1.class); 
      startActivity(intent); 

     } 

    }); 
    // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 

} 
}); // >>>>>>>>>> button one ends here 

另一種簡單的方式來聲明onClickListener是將其聲明爲對象場像下面

private View.OnClickListener clickListener = new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      switch (v.getId()){ 
       case R.id.notelist1: { 
        // go to notelist 1 
        break; 
       } 

       case R.id.notelist: { 
        // go to notelist 
        break; 
       } 
      } 
     } 
    }; 

並設置監聽器上的按鈕類似下面

buttonOne.setOnClickListener(clickListener); 
buttonTwo.setOnClickListener(clickListener);