Android编程帮助与文本字段有关

时间:2015-10-05 15:23:52

标签: android

我正在创建拨号盘,但问题是我不擅长android编程我需要一些帮助,我无法在编辑文本中输入如果我将clicklistner放在一个按钮上它显示结果但是当我做同样的第二个按钮它删除了以前的输入,例如我想从拨号盘获得0334534 .. num这样但是当我点击1它打印1但是当我点击2它打印2但是替换1

{

package com.example.haseeb.dialpad;

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.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends Activity {

Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final TextView textView=(TextView)findViewById(R.id.editText);
        final Button button1 =  (Button)findViewById(R.id.button1);
        final Button button2 =  (Button)findViewById(R.id.button2);


        final TextView textView2=(TextView)findViewById(R.id.editText);



        //button=(Button)findViewById(R.id.button1);
        //button.setOnClickListener(this);

        button1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click
                textView.setText("1");

            }         });




    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


}

}

2 个答案:

答案 0 :(得分:0)

每次单击按钮时都需要添加文本,如下所示:

textView.setText("1" + textView.getText());

答案 1 :(得分:0)

您可以使用append()方法

更改为onClick()方法内的

textView.append("1");