我无法让Android onClick
事件发挥作用。我在activity_main.xml中渲染了一个TextView和一些按钮。在MainActivity.java中,我尝试将onClick
事件附加到将其值附加到输入字段的所有按钮。但是,我很难找到关于制作拨号器工作主题的教程,书籍等。我有三本书,没有谈论使用通话功能或制作拨号器。我在网上找到的教程都是关于人们向用户展示如何根据和设置他们的拨号器,而不是如何编程。所以我的问题:
1)我在文本视图中附加值时出错了吗? 2)如何使用del onClick事件删除最后一个值? 3)当用户点击键盘时,如何阻止文本视图拉起键盘?即如何禁用/只读(也许)只有按钮可以更新它?
感谢您的帮助。在下面的代码中,我将按钮切割为一个。我所做的就是复制所有按钮并更改ID,文本等。所以所有方法都是相同的,但我不能只使用一个按钮。再一次,请不要为下面的代码嘲笑我。
activity_main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/title_two"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:maxLength="15" >
<requestFocus />
</EditText>
<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="fill_parent" >
<Button
android:id="@+id/one"
android:layout_width="93dp"
android:layout_height="wrap_content"
android:text="1" />
<Button
android:id="@+id/two"
android:layout_width="93dp"
android:layout_height="wrap_content"
android:text="2" />
<Button
android:id="@+id/three"
android:layout_width="93dp"
android:layout_height="wrap_content"
android:text="3" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="fill_parent" >
<Button
android:id="@+id/four"
android:layout_width="93dp"
android:layout_height="wrap_content"
android:text="4" />
<Button
android:id="@+id/five"
android:layout_width="93dp"
android:layout_height="wrap_content"
android:text="5" />
<Button
android:id="@+id/six"
android:layout_width="93dp"
android:layout_height="wrap_content"
android:text="6" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="fill_parent" >
<Button
android:id="@+id/seven"
android:layout_width="93dp"
android:layout_height="wrap_content"
android:text="7" />
<Button
android:id="@+id/eight"
android:layout_width="93dp"
android:layout_height="wrap_content"
android:text="8" />
<Button
android:id="@+id/nine"
android:layout_width="93dp"
android:layout_height="wrap_content"
android:text="9" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="fill_parent" >
<Button
android:id="@+id/star"
android:layout_width="93dp"
android:layout_height="wrap_content"
android:text="*" />
<Button
android:id="@+id/zero"
android:layout_width="93dp"
android:layout_height="wrap_content"
android:text="0" />
<Button
android:id="@+id/pound"
android:layout_width="93dp"
android:layout_height="wrap_content"
android:text="#" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="fill_parent" >
<Button
android:id="@+id/callButton"
android:layout_width="93dp"
android:layout_height="wrap_content"
android:text="Call" />
<Button
android:id="@+id/contacts"
android:layout_width="93dp"
android:layout_height="wrap_content"
android:text="Con" />
<Button
android:id="@+id/del"
android:layout_width="93dp"
android:layout_height="wrap_content"
android:text="Del" />
</LinearLayout>
</LinearLayout>
和MainActivity.java:
package com.example.dialertest
import android.app.Activity;
import android.content.Intent;
import android.content.ActivityNotFoundException;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
Button oneBtn;
Button twoBtn;
Button threeBtn;
Button fourBtn;
Button fiveBtn;
Button sixBtn;
Button sevenBtn;
Button eightBtn;
Button nineBtn;
Button starBtn;
Button zeroBtn;
Button poundBtn;
Button callBtn;
Button conBtn;
Button delBtn;
EditText numTxt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
oneBtn = (Button) findViewById(R.id.one);
twoBtn = (Button) findViewById(R.id.two);
threeBtn = (Button) findViewById(R.id.three);
fourBtn = (Button) findViewById(R.id.four);
fiveBtn = (Button) findViewById(R.id.five);
sixBtn = (Button) findViewById(R.id.six);
sevenBtn = (Button) findViewById(R.id.seven);
eightBtn = (Button) findViewById(R.id.eight);
nineBtn = (Button) findViewById(R.id.nine);
starBtn = (Button) findViewById(R.id.star);
zeroBtn = (Button) findViewById(R.id.zero);
poundBtn = (Button) findViewById(R.id.pound);
callBtn = (Button) findViewById(R.id.callButton);
conBtn = (Button) findViewById(R.id.contacts);
delBtn = (Button) findViewById(R.id.del);
numTxt = (EditText) findViewById(R.id.editText1);
oneBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
oneBtn.setText("1");
numTxt.setText(numTxt.getText() + " " + oneBtn.getText());
//numTxt.append("1");
}
});
/**private void performDial(String numberString) {
if (!numberString.equals("")) {
Uri number = Uri.parse("tel:" + numberString);
Intent dial = new Intent(Intent.ACTION_CALL, number);
startActivity(dial);
}
}*/
/*private void call() {
try {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel: 8880000000"));
startActivity(callIntent);
} catch(ActivityNotFoundException e) {
Log.e("Dialed Number","Call Failed", e);
}
}
}*/
}
}
答案 0 :(得分:1)
将onClicklistener更改为:
oneBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// no need to use onBtn here cuz the param v is the clicked View
Button clickedButton = (Button) v;
clickedButton.setText("1");
numTxt.setText(numTxt.getText().toString() + " " + clickedButton.getText());
//numTxt.append("1");
}
});
Edittext的getText()方法返回一个可编辑对象,请参阅:
http://developer.android.com/reference/android/widget/EditText.html#getText() http://developer.android.com/reference/android/text/Editable.html
答案 1 :(得分:1)
要获取edittext
的文字,您必须在toString()
之后致电getText()
numTxt.setText(numTxt.getText().toString() + " " + clickedButton.getText());