按钮点击事件issure,布局和解析问题? [英] Button click event issure, layout,and parsing issue?
问题描述
有我面临的三个问题:
-
当我点击(+)按钮,编辑框会在按钮下方,而我希望它们的上方。
当前显示:
想是这样的:
<编辑文本1> <编辑文本2>
<编辑文字3> <编辑文本4>
<编辑text5> <编辑text6>
<编辑text7> <编辑text8> <按钮和GT; <&按钮2 GT;
- 正如你所看到的,我试图解析这是我从XML了,到docalc()函数编辑文本值,并显示在文本框中的值。但它不工作了。显示我什么都没有。
- 我可以分析值dostuff,如果是我将如何相互关联那些docalc?
在此先感谢...
的Java code:
进口android.app.Activity;进口android.os.Bundle;
进口android.view.Gravity;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.view.ViewGroup.LayoutParams;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.LinearLayout;
进口android.widget.TextView; 公共类PlusbuttonActivity扩展活动
实现OnClickListener { TextView的TT;
TextView的焦耳;
的EditText amount1;
的EditText AMOUNT2;
双X = 0;
双Y = 0;
双一= 0;
双Z = 0;
双B = 0;
按钮btnButton;
/ **当第一次创建活动调用。 * /
私人的LinearLayout根;
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
//修改
amount1 =(EditText上)findViewById(R.id.edittext1);
AMOUNT2 =(EditText上)findViewById(R.id.edittext2);
//修改 查看btnButton =(按钮)findViewById(R.id.button_next);
按钮mButton =(按钮)findViewById(R.id.button);
mButton.setGravity(Gravity.CENTER); TT =(的TextView)findViewById(R.id.tt);
J =(的TextView)findViewById(R.id.j); 根=(的LinearLayout)findViewById(R.id.linearLayout);
mButton.setOnClickListener(本);
} @覆盖
公共无效的onClick(视图v){
开关(v.getId()){
案例R.id.button:
查看查看= doStuff();
addViewToRoot(视图);
打破;
案例R.id.button_next:
查看视图1 = doCalc();
addViewToRoot(视图1);
打破; }
} 私人查看doCalc(){
// TODO自动生成方法存根
X = Double.parseDouble(amount1.getText()的toString());
Y = Double.parseDouble(amount2.getText()的toString());
Z =(X * 703);
tt.setText(Double.toString(Z));
返回TT;
} 私人查看doStuff(){
的EditText T =新的EditText(PlusbuttonActivity.this);
t.setGravity(Gravity.LEFT);
t.setWidth(250);
一个的EditText =新的EditText(PlusbuttonActivity.this);
a.setGravity(Gravity.RIGHT);
a.setWidth(250);
的LinearLayout L =新的LinearLayout(PlusbuttonActivity.this); t.setLayoutParams(新的LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
// t.setBackgroundColor(0xffCECECE);
a.setLayoutParams(新的LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); l.setLayoutParams(新的LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); l.addView(T);
l.addView(一); 返回升;
} 私人无效addViewToRoot(视图v){
root.addView(五);
} }
XML文件:
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:方向=垂直
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:ID =@ + ID / LinearLayout中>
<的LinearLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
> <的TextView
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:重力=左
机器人:layout_weight =1
机器人:文字=单位
机器人:ID =@ + ID /单位
/>
< TextView的机器人:layout_height =WRAP_CONTENT
机器人:重力=右
机器人:layout_width =WRAP_CONTENT
机器人:layout_weight =1
机器人:文字=等级
机器人:ID =@ + ID / J>< / TextView的>
< / LinearLayout中>
<的LinearLayout机器人:ID =@ + ID / LinearLayout01
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:方向=横向> <的EditText
机器人:layout_width =250像素
机器人:layout_height =WRAP_CONTENT
机器人:ID =@ + ID / edittext1>
< /&的EditText GT; <的EditText
机器人:layout_height =WRAP_CONTENT
机器人:ID =@ + ID / edittext2的android:layout_width =150dp>
< /&的EditText GT;
< / LinearLayout中> <的LinearLayout机器人:ID =@ + ID / LinearLayout01
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:方向=横向> <的EditText
机器人:layout_width =250像素
机器人:layout_height =WRAP_CONTENT
机器人:ID =@ + ID / edittext3>
< /&的EditText GT; <的EditText
机器人:layout_height =WRAP_CONTENT
机器人:ID =@ + ID / edittext4的android:layout_width =150dp>
< /&的EditText GT;
< / LinearLayout中><的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT> <按钮的android:layout_height =WRAP_CONTENT
机器人:比重=中心机器人:ID =@ + ID /按钮机器人:文本=+的android:layout_width =WRAP_CONTENT>< /按钮> <按钮
机器人:ID =@ + ID / button_next
机器人:layout_height =WRAP_CONTENT
机器人:layout_width =WRAP_CONTENT
机器人:layout_alignParentRight =真
机器人:文字=计算
>
< /按钮>
< / RelativeLayout的>
<的TextView的android:文本=TextView的
机器人:ID =@ + ID / TT
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT>< / TextView的> < / LinearLayout中>
首先,你的LinearLayout的ID设置为除主以外的东西。像根
。主要是XML文件包含您的布局,的LinearLayout项目不是ID名称。
二,而不是使用这个
中的构造函数的EditText的,使用 PlusbuttonActivity.this
。因此改线使用创建的EditText这样:
的EditText T =新的EditText(PlusbuttonActivity.this);
您,当您在onclick方法的技术上的OnClickListener类,这个
指OnClickListener对象是这样做,因为。通过使用 PlusbuttonActivity.this
你澄清你的意思是你当前所在的 PlusbuttonActivity
对象,而不是OnClickListener。
三,以解决其他问题,您不能引用根
从你匿名OnClickListener类。而不是调用 root.addView(t)的
,可以提取出来的方法。全部放在一起,我们得到这样的:
进口android.app.Activity;进口android.os.Bundle;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.view.ViewGroup.LayoutParams;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.LinearLayout;公共类PlusbuttonActivity延伸活动{
/ **当第一次创建活动调用。 * /
的LinearLayout根;
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main); 按钮mButton =(按钮)findViewById(R.id.button1);
根=(的LinearLayout)findViewById(R.id.root);
mButton.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
的EditText T =新的EditText(PlusbuttonActivity.this);
t.setLayoutParams(新的LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
addViewToRoot(T);
}
}); } 私人无效addViewToRoot(视图v){
root.addView(五);
}
}
There are three problems I am facing:
When I click(+)button, the edit boxes are going underneath the button, whereas i want them to be displayed above. currently displaying:
Want like this:
<edit text1> <edit text2>
<edit text3> <edit text4>
<edit text5> <edit text6>
<edit text7> <edit text8>
<button> <button2>
- As you can see, I tried parsing the edit text value which i got from the xml, into the docalc() function, and displaying the value in textbox. but its not working out. showing me nothing.
- can I parse values in dostuff, if yes how will i inter-relate those in docalc?
Thanks in advance...
Java Code:
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
public class PlusbuttonActivity extends Activity
implements OnClickListener {
TextView tt;
TextView j;
EditText amount1;
EditText amount2;
double x=0;
double y=0;
double a=0;
double z=0;
double b=0;
Button btnButton;
/** Called when the activity is first created. */
private LinearLayout root;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// modified
amount1=(EditText)findViewById(R.id.edittext1);
amount2=(EditText)findViewById(R.id.edittext2);
// modified
View btnButton =(Button) findViewById(R.id.button_next);
Button mButton = (Button) findViewById(R.id.button);
mButton.setGravity(Gravity.CENTER);
tt=(TextView)findViewById(R.id.tt);
j=(TextView)findViewById(R.id.j);
root = (LinearLayout) findViewById(R.id.linearLayout);
mButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
View view = doStuff();
addViewToRoot(view);
break;
case R.id.button_next:
View view1 = doCalc();
addViewToRoot(view1);
break;
}
}
private View doCalc() {
// TODO Auto-generated method stub
x=Double.parseDouble(amount1.getText().toString());
y=Double.parseDouble(amount2.getText().toString());
z=(x*703);
tt.setText(Double.toString(z));
return tt;
}
private View doStuff() {
EditText t = new EditText(PlusbuttonActivity.this);
t.setGravity(Gravity.LEFT);
t.setWidth(250);
EditText a = new EditText(PlusbuttonActivity.this);
a.setGravity(Gravity.RIGHT);
a.setWidth(250);
LinearLayout l = new LinearLayout(PlusbuttonActivity.this);
t.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
// t.setBackgroundColor(0xffCECECE);
a.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
l.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
l.addView(t);
l.addView(a);
return l;
}
private void addViewToRoot(View v){
root.addView(v);
}
}
xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/linearLayout">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:layout_weight="1"
android:text="Units"
android:id="@+id/Units"
/>
<TextView android:layout_height="wrap_content"
android:gravity="right"
android:layout_width="wrap_content"
android:layout_weight="1"
android:text="Grades"
android:id="@+id/j"></TextView>
</LinearLayout>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="250px"
android:layout_height="wrap_content"
android:id="@+id/edittext1">
</EditText>
<EditText
android:layout_height="wrap_content"
android:id="@+id/edittext2" android:layout_width="150dp">
</EditText>
</LinearLayout>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="250px"
android:layout_height="wrap_content"
android:id="@+id/edittext3">
</EditText>
<EditText
android:layout_height="wrap_content"
android:id="@+id/edittext4" android:layout_width="150dp">
</EditText>
</LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button android:layout_height="wrap_content"
android:gravity="center" android:id="@+id/button" android:text="+" android:layout_width="wrap_content"></Button>
<Button
android:id="@+id/button_next"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:text="CALCULATE"
>
</Button>
</RelativeLayout>
<TextView android:text="TextView"
android:id="@+id/tt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
</LinearLayout>
First, set your LinearLayout's id to something other than main. Like root
. Main is the name of the xml file containing your layout, not the id of the LinearLayout item.
Second, Instead of using this
in the constructor for the EditText, use PlusbuttonActivity.this
. So change the line you use to create the EditText to this:
EditText t = new EditText(PlusbuttonActivity.this);
You have to do this because when you're in the onClick method your technically in the OnClickListener class, and this
refers to the OnClickListener object. By using PlusbuttonActivity.this
you're clarifying that you mean the PlusbuttonActivity
object that you're currently in, not the OnClickListener.
Third, to address the other issue, you can't reference root
from you anonymous OnClickListener class. Instead of calling root.addView(t)
, you can extract that out to a method. Putting it all together we get this:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
public class PlusbuttonActivity extends Activity {
/** Called when the activity is first created. */
LinearLayout root;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button mButton = (Button) findViewById(R.id.button1);
root = (LinearLayout) findViewById(R.id.root);
mButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
EditText t = new EditText(PlusbuttonActivity.this);
t.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
addViewToRoot(t);
}
});
}
private void addViewToRoot(View v){
root.addView(v);
}
}
这篇关于按钮点击事件issure,布局和解析问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!