如何选择Android OnClick方法



如何通过

xml布局文件选择一种称为"最爱"的方法
android:onClick 

XML方法?

"最爱"方法位于这里:

tk.talcharnes.popularmovies.MovieDetailsFragment

我尝试了很多事情,但是什么都没有用!

这是最喜欢的代码

/**
 * A placeholder fragment containing a simple view.
 */
public class MovieDetailsFragment extends Fragment {
public MovieDetailsFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_movie_details, container, false);
    //get movie object in order to extract details
    Intent intent = getActivity().getIntent();
    int movie_number = intent.getIntExtra("Movie_number", 0);
    MovieModel movie = PostersFragment.getMovieModelList().get(movie_number);
    //set title in details view
    TextView titleView = (TextView) rootView.findViewById(R.id.movie_details_text);
    titleView.setText(movie.getTitle());
    //set poster into details view
    ImageView poster = (ImageView)rootView.findViewById(R.id.poster);
    Picasso.with(getContext()).load(movie.getPoster_path()).placeholder(R.drawable.temp_poster).into(poster);
    // set movie year in details view
    TextView release_date = (TextView)rootView.findViewById(R.id.release_date);
    if(movie.getRelease_date().length() > 3){
    release_date.setText(movie.getRelease_date().substring(0,4));}
    else if (movie.getRelease_date() == null){
        release_date.setText("Release date not available");
    }
    else{
        release_date.setText((movie.getRelease_date()));
    };
    //set vote average in details view
    TextView vote_average = (TextView) rootView.findViewById(R.id.vote_average);
    vote_average.setText(movie.getVote_average() + " /10");
    //set overview in details view
    TextView overview = (TextView) rootView.findViewById(R.id.overview);
    overview.setText(movie.getOverview());

    return rootView;
}
public void favorited(){
    CheckBox favorited = (CheckBox) getView().findViewById(R.id.favorite);
    if (favorited.isChecked()){
        Toast.makeText(getContext(), "ITS CHECKED", Toast.LENGTH_SHORT).show();
    }
}
}

如果您在XML中声明android:onClick="favorited",则需要在 activity 中实现以下方法

public void favorited(View view) {
    //handle click
}

重要:该方法需要在活动中。您可以在此答案中找到更多信息

尝试以下代码

<?xml version="1.0" encoding="utf-8"?>
<!-- layout elements -->
<Button android:id="@+id/btn_favorite"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Click me!"
  android:onClick="favorited" />

在您的XML视图中指定onClick方法:

<Button android:id="@+id/mybutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Click me!"
    android:onClick="favorited" />

,然后在您的片段中定义这样的方法:

public void favorited(View v) {
    // does something very interesting
}

请记住,如果没有favorited(View v),您的方法将不会从XML调用。

使用onclick侦听器而代替xml单击声明:在您的XML广告ID中至可单击的元素。

<Button android:id="@+id/btn_favorite"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"/>

和您的片段

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_movie_details, container, false);
     Button faw = (Button) rootView.findViewById(R.id.favorite);
     faw.setOnclickListener(new View.OnClickListener(){
      @Override
       onClick(){
          favorited(); //or do another action
         }
      });
    }

这种方法可以更好地重复使用和更多安全

将其添加到xml

android:onClick="ButtonClick"

,这在代码中

public void ButtonClick(View v) {
favorited();
}

您还可以在同一按钮单击方法中添加多个按钮,只需在其中添加它:

switch (v.getId()) {
  case R.id.button1:
    doSomething1();
    break;
}

所有这些以及更多的地方都可以使用,例如:Android OnClickListener-标识一个按钮

最新更新