我的某个活动中的所有按钮都不起作用。
我使用了android:onClick="openNewCategory"
,当这项工作没有成功时,我也尝试实施一项仍然无效的onClickListener
。然后,我尝试使用android:clickable="true"
明确地使用了它也没有用。
我已经将问题与我的onCreate方法中的代码相关联,因为它在我发表评论时有效,但我无法确切地知道它可能是什么,因为它只是格式化视图。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ScrollView scrollView = new ScrollView(this);
RelativeLayout relativeLayout = new RelativeLayout(this);
ArrayList<Category> categoryArrayList;
try {
categoryArrayList = Utils.loadCategoryList();
for (int i=0; i<categoryArrayList.size(); i++) {
TextView textView = new TextView(this);
textView.setText(categoryArrayList.get(i).getName());
textView.setTextSize(24);
textView.setPadding(10, 10, 10, 10);
textView.setId(i + 1);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
if (i == 0) {} else {
layoutParams.addRule(RelativeLayout.BELOW, i);
}
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_START);
textView.setLayoutParams(layoutParams);
relativeLayout.addView(textView)
}
for (int i=0; i<categoryArrayList.size(); i++) {
EditText editText = new EditText(this);
editText.setId(i + 101);
DecimalFormat decimalFormat = new DecimalFormat("00");
editText.setText(decimalFormat.format(categoryArrayList.get(i).getPercentage()*100));
editText.setTextSize(24);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_END);
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
if (i == 0) {} else {
layoutParams.addRule(RelativeLayout.BELOW, i);
}
editText.setLayoutParams(layoutParams);
relativeLayout.addView(editText);
TextView textView = new TextView(this);
textView.setText("%");
textView.setTextSize(24);
lp2.addRule(RelativeLayout.ALIGN_BOTTOM, i + 101);
lp2.addRule(RelativeLayout.LEFT_OF, i + 101);
textView.setLayoutParams(lp2);
relativeLayout.addView(textView);
}
} catch (IOException e) {
e.printStackTrace();
}
setContentView(R.layout.edit_categories);
RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.edit_categories_main_container);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
layoutParams.addRule(RelativeLayout.ALIGN_TOP);
scrollView.setLayoutParams(layoutParams);
scrollView.addView(relativeLayout);
mainLayout.addView(scrollView);
setContentView(mainLayout);
}
public void openNewCategory (View view) {
Intent openNewCategoryIntent = new Intent(this, MainActivity.class);
startActivity(openNewCategoryIntent);
}
它可能导致这个问题?
解决:
我刚刚完全解决了!我在xml中创建了edit_categories_button_container
根布局。我将mainContainer
定义为java代码中的新布局,然后添加了按钮容器。工作代码块:
setContentView(R.layout.edit_categories); //to make xml elements accessible
RelativeLayout mainLayout = new RelativeLayout(this);
RelativeLayout buttonContainer = (RelativeLayout) findViewById(R.id.edit_categories_button_container);
ViewGroup buttonContainerParent = (ViewGroup) buttonContainer.getParent();
buttonContainerParent.removeView(buttonContainer); //necessary to avoid "child already has parent error"
mainLayout.addView(buttonContainer);
setContentView(mainLayout);
感谢大家的意见!
答案 0 :(得分:1)
您致电:
setContentView()
代码中有3次。你确定你明白它的作用吗?
答案 1 :(得分:0)
看到我试过了,你的问题在这里解决了!在XML中使用onClock属性:
<强> 1。活动主页:
package com.hamadshaikh.budgetmanagement;
import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
public class Home extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ScrollView scrollView = new ScrollView(this);
RelativeLayout relativeLayout = new RelativeLayout(this);
ArrayList<Category> categoryArrayList;
try {
categoryArrayList = Utils.loadCategoryList();
for (int i=0; i<categoryArrayList.size(); i++) {
TextView textView = new TextView(this);
textView.setText(categoryArrayList.get(i).getName());
textView.setTextSize(24);
textView.setPadding(10, 10, 10, 10);
textView.setId(i + 1);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
if (i == 0) {} else {
layoutParams.addRule(RelativeLayout.BELOW, i);
}
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_START);
textView.setLayoutParams(layoutParams);
relativeLayout.addView(textView);
}
for (int i=0; i<categoryArrayList.size(); i++) {
EditText editText = new EditText(this);
editText.setId(i + 101);
DecimalFormat decimalFormat = new DecimalFormat("00");
editText.setText(decimalFormat.format(categoryArrayList.get(i).getPercentage()*100));
editText.setTextSize(24);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_END);
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
if (i == 0) {} else {
layoutParams.addRule(RelativeLayout.BELOW, i);
}
editText.setLayoutParams(layoutParams);
relativeLayout.addView(editText);
TextView textView = new TextView(this);
textView.setText("%");
textView.setTextSize(24);
lp2.addRule(RelativeLayout.ALIGN_BOTTOM, i + 101);
lp2.addRule(RelativeLayout.LEFT_OF, i + 101);
textView.setLayoutParams(lp2);
relativeLayout.addView(textView);
}
} catch (Exception e) {
e.printStackTrace();
}
setContentView(R.layout.activity_home);
RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.edit_categories_main_container);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
layoutParams.addRule(RelativeLayout.ALIGN_TOP);
scrollView.setLayoutParams(layoutParams);
scrollView.addView(relativeLayout);
mainLayout.addView(scrollView);
setContentView(mainLayout);
}
public void openNewCategory (View view) {
Toast.makeText(this,"Click Performed",Toast.LENGTH_SHORT).show();
//Intent openNewCategoryIntent = new Intent(this, MainActivity.class);
//startActivity(openNewCategoryIntent);
}
}
<强> 2。 main_layout:强>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:id="@+id/edit_categories_main_container"
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=".Home">
<Button android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:onClick="openNewCategory"/>
</RelativeLayout>
第3。实用工具类
import java.util.ArrayList;
/**
* Created by hamadshaikh on 13/08/15.
*/
public class Utils {
public static ArrayList<Category> loadCategoryList() {
ArrayList<Category> categories=new ArrayList<Category>();
Category category=new Category();
category.setName("Hamad");
category.setPercentage(99);
categories.add(category);
return categories;
}
}
<强> 4。类别
public class Category {
private String name;
private int percentage;
public String getName() {
return name;
}
public int getPercentage() {
return percentage;
}
public void setName(String name) {
this.name = name;
}
public void setPercentage(int percentage) {
this.percentage = percentage;
}
}
我试过这个,onClock属性在这里工作,我通过显示Toast&#34; Click Performed&#34; on onClick按钮。我希望你的问题现在解决了!