1

我正在使用 android 支持库,以便能够使用来自 Froyo 的片段和 Sherlock 扩展来显示 ActionBar。

我有一个显示三个 textView 和一个按钮的片段,我希望能够根据 Intent 的内容动态地更改按钮文本。问题是当我调用 button.setText(String text) 时,会出现第二个按钮。但是,我在单击它们中的每一个时都调用了 button.getId() ,并且 Id 是相同的。我不知道为什么会这样,所以我很感激一些帮助。

该应用程序在装有 Android 2.3.3 的三星 Galaxy S 上运行。我无法上传屏幕截图,因为我还没有足够的声誉:(

这是代码:

分段

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.actionbarsherlock.app.SherlockFragment;
import com.parse.ParseUser;
import com.tiempoderodar.egdf.EGDF_App;
import com.tiempoderodar.egdf.R;
import com.tiempoderodar.egdf.security.Constants;

/**
 * @author Daniel Leal López
 *
 */
public class ChapterDetailsFragment extends SherlockFragment{

    private Button b;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i("ChapterDetailsFragment", "Created");
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ParseUser user = EGDF_App.getUser(); 
        b = (Button) getSherlockActivity().findViewById(R.id.buttonWatchChapter);
        if (user != null){

            String chapterNumber = "";
            Bundle extras = getSherlockActivity().getIntent().getExtras();
            String s = extras.getString(Constants.CHAPTER_NUMBER_PARSE);
            if((s != null)&&(!s.equals(""))){
//              tvNumber.setText(s);
                chapterNumber = "Chapter_" + s;
                Log.i("Properties", s);
            }
            String seen = user.getString(chapterNumber);
            if((seen != null)&&(!seen.equals(""))&&(seen.equals(Constants.USER_CHAPTER_SEEN))){
                b.setText("Second text: "+getString(R.string.watch_chapter_button_text));
                Log.i("BUTTON CHANGED", "Text changed to watch");
            }               
            else if((seen != null)&&(!seen.equals(""))&&(seen.equals(Constants.USER_CHAPTER_NOT_SEEN))){
                b.setText("Second text: " + getString(R.string.buy_chapter_button_text));
                Log.i("BUTTON CHANGED", "Text changed to buy");
            }else{
                Log.w("DEV ERROR", "Chapter text not obtained");
            }
        }else{
            Log.w("DEV ERROR", "ParseUser is null in EGDF_App!!!");
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.chapter_details, container, false);
        return view;
    }

    public void setText(String item) {
        getSherlockActivity().getSupportActionBar().setTitle(item);
    }

    public void setButtonText(String text){
        b.setText(text);
    }

}

活动

import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.actionbarsherlock.app.SherlockFragmentActivity;

public class ChapterDetailsActivity extends SherlockFragmentActivity {

    private Bundle extras;
    MediaPlayer mMediaPlayer;

    private String chapterNumber;
    private boolean isChapterSeen;
    //  private int duration;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        setTheme(R.style.Theme_Sherlock_Light_DarkActionBar);

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_chapter_details);

        if (savedInstanceState == null) {
            // During initial setup, plug in the details fragment.
            ChapterDetailsFragment details = new ChapterDetailsFragment();
            details.setArguments(getIntent().getExtras());
            getSupportFragmentManager().beginTransaction().add(
                    android.R.id.content, details).commit();
        }

    }

    public void watchChapter(View view){
        Log.i("Button", "Watch chapter button PRESSED");
        Button b = (Button) view;

        Log.d("BUTTON PARENT VIEW", b.getParent().getClass().getName());
        Log.d("BUTTON ID", String.valueOf(b.getId()));
        String loadChapter = getString(R.string.load_chapter_button_text);
        String watchChapter = getString(R.string.watch_chapter_button_text);
        String buyChapter = getString(R.string.buy_chapter_button_text);

    }

    @Override
    protected void onPause() {
        Log.i("Activity lifecycle", "On pause called");
        super.onPause();
    }

    @Override
    protected void onStop() {
        Log.i("Activity lifecycle", "On stop called");
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        Log.i("Activity lifecycle", "On destroy called");
        super.onDestroy();
        EGDF_App.releaseMediaPlayer();
    }

    @Override
    protected void onResume() {
        Log.i("Activity lifecycle", "On resume called");
        super.onResume();
    }

    @Override
    protected void onStart() {
        Log.i("Activity lifecycle", "On start called");
        super.onStart();
    }
}

活动布局

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

    <fragment
        android:id="@+id/chapterDetailsFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.tiempoderodar.egdf.content.ChapterDetailsFragment" />

</LinearLayout> 

片段布局

<?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="vertical" >

    <TextView
        android:id="@+id/textViewChapterNumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="30dip" />

    <TextView
        android:id="@+id/textViewChapterSeason"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="30dip" />

    <TextView
        android:id="@+id/textViewChapterSinopsis"
        android:layout_width="wrap_content"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="30dip" />

    <TextView
        android:id="@+id/textViewChapterCredits"
        android:layout_width="wrap_content"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="30dip" />

    <Button 
        android:id="@+id/buttonWatchChapter"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="@string/watch_chapter_button_text"
        android:layout_gravity="center"
        android:onClick="watchChapter"/>

</LinearLayout>

谢谢!

4

1 回答 1

0

I think I might have found the problem. I was calling

b = (Button) getSherlockActivity().findViewById(R.id.button); 

but it should be

b = (Button) getView().findViewById(R.id.button);

With that change it works correctly

于 2012-09-15T22:03:22.003 回答